0

我正在编写按钮单击的代码,使用文件对话框打开文件,我可以从中选择图片。然后我想提取文件的路径并将其存储在一个字符串变量中并将其作为参数传递(这里编译器抛出异常:“System.Drawing.dll 中发生'System.IO.FileNotFoundException'类型的第一次机会异常,附加信息:好的”),至于我的代码,我需要动态路径,这样每次类似的图片都不会出现..

//从文件中选择图片
public void select_image_button17_Click(object sender, EventArgs e) {

            foreach (Button b in game_panel1.Controls)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                 openFileDialog1.Filter = "JPG|*.jpg;*.jpeg|PNG|*.png";
                string a = "";
                a = openFileDialog1.ShowDialog().ToString();
                string directoryPath = Path.GetDirectoryName(a);

                Image ToBeCropped = Image.FromFile(a,true);//exception
                ReturnCroppedList(ToBeCropped, 320, 320);
                pictureBox1.Image = ToBeCropped;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                AddImagesToButtons(images);

            }
    }
4

1 回答 1

1

FileName当对话框返回 OK 状态时,将设置该属性。

if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
    // User cancelled out of dialog
}
else
{
    string filename = openFileDialog1.FileName;
}
于 2017-05-16T23:09:02.137 回答