为PictureBox设置Dedault图片并保存为C#

问题描述 投票:0回答:1

我想知道如何从项目文件夹中获取图片,并且如果用户不选择任何图片,我可以将其另存为默认的个人图片

我使用此代码创建名称,路径和保存(如果我有任何图片传送,如果用户未选择任何图片,则我的代码损坏了

                //Set Image Name                    
                string imageName = txtNationalCode.Text.ToString() + Path.GetExtension(imgPersonalPhoto.ImageLocation);
                //Set Image Path
                string ImageLocation = imgPersonalPhoto.ImageLocation;
                //Save Image 
                string path = Application.StartupPath + "/Images/";
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                try
                {
                    if (imgPersonalPhoto.Image != null)
                    {
                        imgPersonalPhoto.Image.Save(path + imageName);
                    }
                    else
                    {
                        //i dont know how set some picture for default and save it ! ( i have 1 picture for background picturebox

                        imgPersonalPhoto.Image.Save(path + imageName);
                    }
                }
                catch
                {
                    RtlMessageBox.Show("Add Picture for this Personal please ");
                }

我将尝试在我的应用程序中添加一些图片,并从我的应用程序文件夹中的文件中加载数据(但不起作用,或者我不知道我必须在哪里放置图片)

c# image picturebox
1个回答
0
投票

使用Image.FromFile在Form_Load中设置图像。这样,它将在加载表单时始终设置默认图像。

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.Image = Image.FromFile(@"C:\...");
}
© www.soinside.com 2019 - 2024. All rights reserved.