tags:

views:

272

answers:

4

how can i get an image that i drawn on a picturebox? in my application i created a form with the freehand drawing.but i can't save the image that i drawn as a file,there have a error message that "object reference not set to an instance of an object".I am using the code

me.picturebox1.image.save(filename,sysytem.....jpeg)

any one knows please help me...thank you

A: 

Hope this post helps

How to save image to file?

rahul
+1  A: 

There appears to be nothing intrinsically wrong with your code (unless the problem is with one of the parameters). PictureBox.Image.Save() does work!

It is possible that the PictureBox displays an image, but the Image object referenced by it is already disposed. You can get rid of the Object reference error by implementing a check for nullability first :

If picturebox1.Image isnot Nothing Then
  ...
End If

In addition, you should check the code that draws the Image... does it dispose of references as expected ?

Cerebrus
A: 

Hi,

I tried a similar operation.

pictureBox1.Image.Save("C:\\tempStack.jpg", ImageFormat.Jpeg);

The cause could be that the pictureBox1 (very unlikely) or pictureBox1.Image is not set.

Use

pictureBox1.Load("")

to load the image before trying to save it.

Koekiebox
Not set meaning Nothing or Null reference.
Koekiebox
+1  A: 

I will assume that you never assign the Image property of your PictureBox but instead perform drawing commands directly on a Graphics object retrieved from the PictureBox control. If that is the case, the Image property will remain Nothing/null when you perform the drawing. To get around this, assign an empty image to the Image property when you initialize the PictureBox control:

myPictureBox.Image =  New Bitmap(myPictureBox.ClientSize.Width, _
                                 myPictureBox.ClientSize.Height)

Then, when you want to perform some drawing, use a Graphics object from the Image instead of the PictureBox:

Using g As Graphics = Graphics.FromImage(myPictureBox.Image)
    ' do the drawing '
End Using

With this approach you can save the image as you do in your current code, by simply calling Save on the PictureBox.Image object.

Fredrik Mörk
thanks for your answer.but please tell me where can i place this code
@sinoydevassy: that really depends on the structure of your application, but if you have a point of the code where you initialize a "new drawing", that would be the place for assigning the new, empty `Bitmap` object.
Fredrik Mörk
thank u so much.it's working good.i initialize in the form load.thank you.but there have a problem.the saved image's background color is everytime black.so i cant see my image because my drawing pen color is also black.thank you