Line drawing on picturebox

cyphixation

Member
Joined
Jun 25, 2003
Messages
7
Hi guys. im pretty new to vb.net and graphics programming and have a form with a picturebox and one button. im just wanting to draw a few lines from where the mouse button goes down to where it comes up.

The pic stays white the whole time in run mode, but stopping at a breakpoint at the end of PictureBox1_MouseUp() in debug, i can see the lines.

Can someone help me out and more so point me to tutorial sites on vb.net graphics? The code is below.

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim big_G As Graphics
    Dim pen As New pen(Red)
    Dim x1, y1, x2, y2 As Integer
    Dim DRAW As Boolean
    Public Const YES As Boolean = True
    Public Const NO As Boolean = False


    Private Sub image_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                Handles image_new.Click
        Dim bmp As Bitmap
        Dim G As Graphics

        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Image = bmp
        G = Graphics.FromImage(bmp)
        G.Clear(White)

        big_G = G
    End Sub


    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
                                      Handles PictureBox1.MouseDown
        x1 = e.X
        y1 = e.Y
        DRAW = YES
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
                                      Handles PictureBox1.MouseUp
        x2 = e.X
        y2 = e.Y

        If (DRAW) Then
            big_G.DrawLine(pen, x1, y1, x2, y2)
            DRAW = NO
        End If
    End Sub
End Class

Sorry, im not sure how to correctly post code here either :(
 
Last edited by a moderator:
damn...well, it doesnt repaint itself if min/maxed or covered with another app. i supose i have more reading to do, but thank you very much for the start :)
 
I got it working.....
Hope this helps!

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim bmp As Bitmap
    Dim big_G As Graphics
    Dim pen As New Pen(Color.Red)
    Dim x1, y1, x2, y2 As Integer
    Dim DRAW As Boolean
    Public Const YES As Boolean = True
    Public Const NO As Boolean = False

    Private Sub image_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles image_new.Click
        Dim G As Graphics
        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Image = bmp
        G = Graphics.FromImage(bmp)
        G.Clear(Color.White)
        big_G = G
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseDown
        x1 = e.X
        y1 = e.Y
        DRAW = YES
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles PictureBox1.MouseUp
        x2 = e.X
        y2 = e.Y
        If (DRAW) Then
            big_G.DrawLine(pen, x1, y1, x2, y2)
            DRAW = NO
            PictureBox1.Image = Nothing
            PictureBox1.Image = bmp
        End If
    End Sub

End Class
 
Last edited by a moderator:
.....and if you dont want to use the Bitmap and PictureBox method you should take a look at the Controls "Paint" event! this is the place where all the drawing is done normally....
 
Back
Top