简体   繁体   中英

Access denied to the path when uploading an image to a picturebox

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.

您必须使用选定的文件名创建图像资源,并将其提供给属性Image,如下所示:

pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM