为什么我的图像在图像框中加载时为空?

时间:2015-12-19 17:10:50

标签: c#

MemoryStream stream = new MemoryStream();
// coverImagePictureBox.Image.Save(coverImagePictureBox.ImageLocation, System.Drawing.Imaging.ImageFormat.Jpeg);    
coverImagePictureBox.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
  

NullReferenceException

我正在以表单形式加载图片,但是当我调用imagebox.image时,虽然图片正确加载,但它仍会返回null

这就是我加载图片的方式

OpenFileDialog dlg = new OpenFileDialog();
        dlg.Filter = "JPG Files(*.jpg|*.jpg|*.PNG Files(*.png|*.png|All Files(*.*)|*.*";
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            string picpath = dlg.FileName.ToString();
            textBox1.Text = picpath;
            coverImagePictureBox.ImageLocation = picpath;

1 个答案:

答案 0 :(得分:0)

该应用程序告诉您存在NullReferenceException,表明您尝试操作的内容为null。现在在你的代码中,唯一能够为null的是PictureBox的Image。

当然,您永远不会将图像分配给PictureBox,当您访问它的图像时,不可避免地会抛出Null Reference Exception。

所以,仔细检查一下。另一件事可能是您实际上正在分配BackgroundImage但尝试访问Image。