Link to home
Start Free TrialLog in
Avatar of FragReaper
FragReaper

asked on

VB .net 2008 Draw in PictureBox and save result to bitmap

Good morning all, I'm coding a project in visual basic 2008 professional. In essence this part of the program captures a signature drawn on the screen by a digitizer, I want to capture this to a bitmap file so I can do things with it later. I have the code for drawing ok but I cant get the code to save the result to  a file to work. Solutions sought! The output file is the correct dimensions but blank, with no drawing.
' (this bit works, drawing on the picturebox)
 Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
 
        Dim localmouseposition As Point
        localmouseposition = PictureBox1.PointToClient(Cursor.Position)
        x2 = xbox.Text ''
        y2 = ybox.Text '' 
        xbox.Text = localmouseposition.X ''
        ybox.Text = localmouseposition.Y ''
        x1 = xbox.Text '' 
        y1 = ybox.Text '' ^^^ Code takes pointAPI for mouse position on signature box,
        '_ shunts previous location to buffer
 
 
        If doidraw = True Then
            Dim g As System.Drawing.Graphics
           
            g = PictureBox1.CreateGraphics
            g.DrawLine(Pens.Black, x1, y1, x2, y2)
            PictureBox1.Update()
 
 
        End If
 
    End Sub
 
'(this bit doesn't work)
        PictureBox1.Update()
        Dim bmp As New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
        bmp.Save("c:\temp\test.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FragReaper
FragReaper

ASKER

Excellent! Thanks
How would you use this code to:

1) Increase the width of the pen drawing

2) Change the color of the pen

Thanks!

David
In the Paint() event, it is using the standard black pen via "Pens.Black".

You can simply create your own pen where you specify the color and width:
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If Not IsNothing(signature) Then
            Using marker As New Pen(Color.Green, 3)
                e.Graphics.DrawPath(marker, signature)
            End Using
        End If
    End Sub

Open in new window

Awesome, Idle_Mind!

Thanks! :o)

David