为 PictureBox 设置 Dedault Picture 并将其保存为 C#



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

我使用此代码创建名称,路径和保存(如果我有任何图片很好,如果用户不选择任何图片,我的代码坏了

//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 ");
}

我将尝试在我的应用程序文件夹中添加一些图片 但是如果我将我的应用程序发送到其他文件夹,则不存在!

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

private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"C:...");
}

最新更新