how to save drawing image?

albertkhor

Well-known member
Joined
Jan 12, 2006
Messages
150
Programming Experience
Beginner
i have create a system which allows user to draw an image and save it.

i use panel and SaveFileDialog in this system. i already done the drawing path which allow user to use mouse to draw whatever they want in panel but i do not know how to save the image in .bmp format, can someone teach me or provide me code how to save image?
 
You would create a Bitmap object, create a Graphics object for it and then use basically the same code as you used to draw on the Panel to draw on the Bitmap. You'd then call its Save method.
VB.NET:
        Dim sfd As New SaveFileDialog

        If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim bm As New Bitmap(Me.Panel1.Width, Me.Panel1.Height)
            Dim g As Graphics = Graphics.FromImage(bm)

            'Use g to draw on bm here.

            g.Dispose()
            bm.Save(sfd.FileName)
        End If

        sfd.Dispose()
 
You may need to add a second parameter to the call to Save to specify the BMP format but I'd guess that it would be the default.
VB.NET:
bm.Save(sfd.FileName, Imaging.ImageFormat.Bmp)
 
jmcilhinney said:
You would create a Bitmap object, create a Graphics object for it and then use basically the same code as you used to draw on the Panel to draw on the Bitmap. You'd then call its Save method.
VB.NET:
        Dim sfd As New SaveFileDialog
 
        If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim bm As New Bitmap(Me.Panel1.Width, Me.Panel1.Height)
            Dim g As Graphics = Graphics.FromImage(bm)
 
            'Use g to draw on bm here.
 
            g.Dispose()
            bm.Save(sfd.FileName)
        End If
 
        sfd.Dispose()

jmcilhinney thanks, my system can work now!
 
Last edited:
Back
Top