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 ha
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 ?
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.
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.