Help - Graphics to Image

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
2 posts Page 1 of 1
Contributors
User avatar
Wusami
New Member
New Member
Posts: 21
Joined: Mon Feb 13, 2012 8:00 pm

Help - Graphics to Image
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
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Help - Graphics to Image
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
2 posts Page 1 of 1
Return to “Coding Help & Support”