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.