未处理的类型为'System.ArgumentException'发生

本文关键字:ArgumentException 发生 System 未处理 类型 | 更新日期: 2023-09-27 18:09:25

类型为'System '的未处理异常。在mscorlib.dll中发生了ArgumentException,附加信息:路径不是合法的形式。只有在关闭窗口后第二次按下时才会出现错误。

这是代码:

        private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog OpenFd = new OpenFileDialog();
        OpenFd.Filter = "Images only. |*.jpg; *.jpeg; *.png; *.gif;";
        DialogResult dr = OpenFd.ShowDialog();
        pictureBox1.Image = Image.FromFile(OpenFd.FileName);
        ItemUrl.Text = OpenFd.FileName;

    }

未处理的类型为'System.ArgumentException'发生

您需要处理是否选择了图像或单击FileDialog上的"Cancel"。请使用以下格式:

        OpenFileDialog OpenFd = new OpenFileDialog();
        OpenFd.Filter = "Images only. |*.jpg; *.jpeg; *.png; *.gif;";
        if (OpenFd.ShowDialog() == DialogResult.OK) //You selected an image
        {
            pictureBox1.Image = Image.FromFile(OpenFd.FileName);
            ItemUrl.Text = OpenFd.FileName;
        }
        else
        {
            //You cancelled the operation
        }