-1
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

I am loading an image in a form but when I'm calling imagebox.image it's returning null although the image is loaded in the form correctly

and this is how i'm loading the image

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;
Bayan
  • 82
  • 6
  • 2
    When you ask a question on Stack Overflow, please do not show code as an image. Copy the code as text into your question, along with the error message. This makes it easier to search and quicker to see your issue. And when you get a NullReferenceException, you should see what's null. – mason Dec 19 '15 at 17:13
  • 2
    show code of how you loaded image into `coverImagePictureBox`. – Igor Dec 19 '15 at 17:21
  • Please post the stacktrace by copy-pasting it, not by uploading a screenshot of ti. – Yassin Hajaj Dec 19 '15 at 17:24
  • You told us twice that you have loaded an image in a form. but you try to save an image from a picturebox. Now which?? The code works fine if indeed you have loaded an image in the picturebox.Image property. Not the Form's or the PictureBox's BackgroundOÍmage property! – TaW Dec 20 '15 at 14:33
  • Yuo save image not load. – Klaudiusz bryjamus Dec 20 '15 at 17:20

1 Answers1

0

The app tells you that there is a NullReferenceException indicating that something you are trying to operate on is null. Now in your code, the only thing capable of being null is the Image of the PictureBox.

Surely you never assign an image to the PictureBox and when you access it's image, inevitably a Null Reference Exception is thrown.

So, double check that. Another thing could be that you are actually assigning the BackgroundImage but trying to access the Image.

Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 5,561
  • 5
  • 26
  • 49