Bitmap Saving

Anticipation

Member
Joined
Jul 15, 2008
Messages
10
Programming Experience
Beginner
I'm trying to save an image drawn onto a PictureBox, using the sub procedure
VB.NET:
 Sub SaveImage(ByVal pb As PictureBox, ByVal sFile As String)
        Dim bmp As New Bitmap(pb.Width, pb.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        Dim pe As New PaintEventArgs(g, New Rectangle(Point.Empty, bmp.Size))
        Me.InvokePaint(pb, pe)
        Dim fmt As Imaging.ImageFormat
        Dim lFile As String = sFile.ToLower
        If lFile.EndsWith(".emf") Then
            fmt = Imaging.ImageFormat.Emf
        ElseIf lFile.EndsWith(".exif") Then
            fmt = Imaging.ImageFormat.Exif
        ElseIf lFile.EndsWith(".gif") Then
            fmt = Imaging.ImageFormat.Gif
        ElseIf lFile.EndsWith(".jpg") OrElse lFile.EndsWith(".jpeg") Then
            fmt = Imaging.ImageFormat.Jpeg
        ElseIf lFile.EndsWith(".png") Then
            fmt = Imaging.ImageFormat.Png
        ElseIf lFile.EndsWith(".tiff") OrElse lFile.EndsWith(".tif") Then
            fmt = Imaging.ImageFormat.Tiff
        ElseIf lFile.EndsWith(".wmf") Then
            fmt = Imaging.ImageFormat.Wmf
        Else
            fmt = Imaging.ImageFormat.Bmp
        End If
        bmp.Save(sFile, fmt)
        g.Dispose()
        bmp.Dispose()
        pe.Dispose()
    End Sub
And then calling it using
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SaveImage(picCanvas, "c:\test.png")
End Sub
The problem is, that on saving i just get a blank png. Any help would be appreciated. Thanks.
 
VB.NET:
Dim r As Rectangle = Me.PictureBox1.ClientRectangle
Dim bmp As New Bitmap(r.Width, r.Height)
Me.PictureBox1.DrawToBitmap(bmp, r)
Select Case will also save you lots of typing and make more readable code:
VB.NET:
Select Case IO.Path.GetExtension(filename).ToLower
    Case ".jpg", ".jpeg"
        format = Imaging.ImageFormat.Jpeg
    Case ".bmp"
        format = Imaging.ImageFormat.Bmp
End Select
 
Thanks for the help :). However, when i save the picture, instead of it just being a blank bitmap, it turns out to be a bitmap with a white backround (although changing the backcolor of the picturebox does save). If it helps any, here's the code i'm using to draw onto the picturebox.
VB.NET:
Dim XPrev, YPrev, XCur, YCur As Integer

Private Sub picCanvas_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        'Setting the previous points for line drawing
        XPrev = e.X
        YPrev = e.Y
    End Sub

    Private Sub picCanvas_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
        'Setting the current points for line drawing
        XCur = e.X
        YCur = e.Y

        'Graphics Variables
        Dim p As New Pen(PenColour, PenWidth)
        Dim g As Graphics = picCanvas.CreateGraphics

        g.DrawLine(p, XPrev, YPrev, XCur, YCur)

        p.Dispose()
        g.Dispose()
    End Sub
Thanks.
 
You can't use CreateGraphics for drawing, you must use the Paint event, the correct Graphics instance is provided through e.Graphics.
 
Again, thanks for the help :). Sorry to bother you again, but this really is starting to bother me. I made a paint event, and put in the code
VB.NET:
Dim p As New Pen(PenColour, PenWidth)
Dim g As Graphics = e.Graphics

g.DrawLine(p, XPrev, YPrev, XCur, YCur)
and nothign happens. Yet it works fine when i change the XPrev, YPrev, XCur, YCur to 50, 50, 150, 175.
Thanks.

EDIT: Solved; i forgot to invalidate the PictureBox.
 
Last edited:
Back
Top