I have a WinForms app (in C#) that consist of a simple form, with a pictureBox and a button on it. Also, I have an openFileDialog control attached to the form.
I want that, when I press the button, the openFileDialog should bring up, and it would allow me to choose an image from my computer, and finally to display it on the pictureBox of my form.
What I have done until now: (this is the click - event handler of my button)
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Deschide fisier";
openFileDialog1.Filter = "Fisiere imagine (*.png) |*.png";
openFileDialog1.FileName = "";
openFileDialog1.InitialDirectory = "MyDocuments";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBox1.ImageLocation = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
pictureBox1.Load();
}
}
I run the app, I press the button, the openFileDialog opens, I choose my image, I press OK, but then I get an exception: Access to the path "D:\" is denied. I tried to move the image on the desktop or in C drive, but I get the same exception.
I don't know how to figure this out.
I use Visual Studio 2013 Ultimate on Windows 8.1.
Thank you respectfully.
You have to create an image resource out of the selected file name and provide it to the property Image like this:
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);