-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; 
5
  • 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. Commented Dec 19, 2015 at 17:13
  • 2
    show code of how you loaded image into coverImagePictureBox. Commented Dec 19, 2015 at 17:21
  • Please post the stacktrace by copy-pasting it, not by uploading a screenshot of ti. Commented Dec 19, 2015 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! Commented Dec 20, 2015 at 14:33
  • Yuo save image not load. Commented Dec 20, 2015 at 17:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The compiler doesnt tell anything, exceptions happen in runtime.
@AlbertoMonteiro You're right. And so.. you found me. Better stop what you're doing. YOU KNOW VERY WELL what you are doing.
So you ask me to stop something, and why you unaccepted my correct answer in your question? stackoverflow.com/questions/34212090/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.