Page 1 of 1

Help - Graphics to Image

Posted: Sat Jul 21, 2012 4:19 pm
by Wusami
Hello i am using this simple code to paint on Picturebox1.creategraphics
Code: Select all
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        'If the left mouse button is down the set IsThePenDown to TRUE  
        If e.Button = Windows.Forms.MouseButtons.Left Then
            IsThePenDown = True
            'Set the Point to start drawing from.>>  
            MouseDownPoint = New Point(e.X, e.Y)
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        'A STATIC variable holds or remebers the previous value.>>  
        Static LastPoint As New Point
        If IsThePenDown = True Then
            'Draw a line from the LastPoint.>>  
            PictureBox1.CreateGraphics.DrawLine(MyPen, LastPoint.X, LastPoint.Y, e.X, e.Y)
        End If
        'Set the LastPoint to the previous point.>>  
        LastPoint = New Point(e.X, e.Y)
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        'Set to FALSE on the MouseUp event.>>  
        IsThePenDown = False
    End Sub
And i wanted to know how to convert this so that it is on picturebox1.image

Re: Help - Graphics to Image

Posted: Sat Jul 21, 2012 5:15 pm
by mandai
You would need to add this code:
Code: Select all
    Dim gfx As Graphics

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        gfx = Graphics.FromImage(PictureBox1.Image)

    End Sub
Then you can alter the PictureBox1_MouseMove event:
Code: Select all
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        'A STATIC variable holds or remebers the previous value.>>  
        Static LastPoint As New Point

        If IsThePenDown = True Then
            'Draw a line from the LastPoint.>>  
            gfx.DrawLine(MyPen, LastPoint.X, LastPoint.Y, e.X, e.Y)
            PictureBox1.Refresh()
        End If
        'Set the LastPoint to the previous point.>>  
        LastPoint = New Point(e.X, e.Y)
    End Sub