简体   繁体   English

将图片上传到图片框时,访问被拒绝访问路径

[英]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. 我有一个WinForms应用程序(在C#中),该应用程序由一个简单的表单组成,上面有一个pictureBox和一个按钮。 Also, I have an openFileDialog control attached to the form. 另外,我在表单上附加了一个openFileDialog控件。

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. 我希望,当我按下按钮时,应该打开openFileDialog,它将允许我从计算机中选择图像,最后将其显示在表单的pictureBox上。

What I have done until now: (this is the click - event handler of my button) 到目前为止,我所做的事情:(这是我的按钮的click-事件处理程序)

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. 我运行该应用程序,按下按钮,打开openFileDialog,选择我的图像,按下OK,但随后出现异常:拒绝访问路径“ D:\\”。 I tried to move the image on the desktop or in C drive, but I get the same exception. 我试图将图像移到桌面或C驱动器中,但是出现了同样的异常。

I don't know how to figure this out. 我不知道该怎么解决。

I use Visual Studio 2013 Ultimate on Windows 8.1. 我在Windows 8.1上使用Visual Studio 2013 Ultimate。

Thank you respectfully. 谢谢你。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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