getting image from a picturebox

后端 未结 3 810
情歌与酒
情歌与酒 2020-12-22 04:45

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

相关标签:
3条回答
  • 2020-12-22 05:27

    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 ?

    0 讨论(0)
  • 2020-12-22 05:32

    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.

    0 讨论(0)
  • 2020-12-22 05:36

    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.

    0 讨论(0)
提交回复
热议问题