基本上,我有一个浏览按钮来打开文件对话框并将图片提取到文本框和图片框中。但是,我希望在用户打开文件对话框之前显示默认图片(如facebook默认个人资料图片)。当用户打开文件对话框时,选择图片并单击“确定”,默认图片将更改为所选图片。如果用户单击“取消”,则默认图片不会更改。
我的问题:
这是我的以下代码:
private void buttonbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "image files|*.jpg;*.png;*.gif";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Cancel)
return;
pictureBoxPhoto.Image = Image.FromFile(ofd.FileName);
textBoxPhoto.Text = ofd.FileName;
}
public Form1() {
InitializeComponent();
pictureBox1.Image = Properties.Resources.DefaultPicture;
}
您可以使用ErrorImage或InitialImage:
pictureBox1.Image = pictureBox1.InitialImage;
pictureBox2.Image = pictureBox2.ErrorImage;