我有这个“ NullReference Exception”,我找不到如何忽略它

时间:2019-07-17 15:25:52

标签: c# winforms

我正在尝试创建用于转换图像形式的WF应用程序

我尝试先检查图像的格式,然后再将其放入pictureBox,但经常会遇到对象引用未设置为对象实例的情况。我不知道为什么!

        OpenFileDialog fd=new OpenFileDialog();

        if (fd.ShowDialog() == DialogResult.OK)
        {
            //I want to insure the user to choose JPG file 
            //the exception pointing out in this line :
            //is this piece of code make sense !
            if (!PictureBox.Image.RawFormat.Equals(ImageFormat.Jpeg)) 
            {
                MessageBox.Show("Please Choose Image with JPG as extention");
            }
        }
        else
        {
              pb_jpg.SizeMode = PictureBoxSizeMode.Zoom;
              pb_jpg.Image = Image.FromFile(fd.FileName);

              lbl_jpgTaille.Text = sizeInkb.ToString();
              lbl_jpgwidth.Text = pb_jpg.Image.Width.ToString();
              lbl_jpgheight.Text = pb_jpg.Image.Height.ToString();
        }

我希望该图像文件不是jpg格式的消息

0 个答案:

没有答案