Reputation: 1154
Basically, I have a browse button to open a file dialog and fetch the picture into a textbox and picturebox. However, I want to have a default picture that shows (like facebook default profile picture) before the user opens the file dialog. When the user opens the file dialog, chooses a picture and clicks OK, the default picture will be changed into the selected picture. If the user clicks Cancel, the default picture won't change.
My question:
Here's my following code:
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;
}
Upvotes: 3
Views: 12519
Reputation: 198
You can use the ErrorImage or the InitialImage:
pictureBox1.Image = pictureBox1.InitialImage;
pictureBox2.Image = pictureBox2.ErrorImage;
Upvotes: 1
Reputation: 18743
I would advise to add your picture into your project Resources:
It will then be stored into the Resources folder of your project:
You can then add it to your picturebox in your form constructor:
public Form1() {
InitializeComponent();
pictureBox1.Image = Properties.Resources.DefaultPicture;
}
Upvotes: 5