Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
XML RSS Feed
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
 
TitleMake a scribble application with VB .NET that automatically redraws when necessary
KeywordsVB.NET, drawing, scribble, redraw, autoredraw
CategoriesGraphics, VB.NET
 
When the form loads or is resized, the AllocateBitmap subroutine resizes the picCanvas PictureBox to fit the form. It then creates a Bitmap to fill the PictureBox and makes a Graphics object to draw on the Bitmap. It then sets the PictureBox's Image property to the Bitmap to display the Bitmap.

In the MouseDown event handler, the program sets m_Drawing to True and saves the current point.

In the MouseMove event handler, the program verifies that m_Drawing is True. If it is, the code draws a line on the Bitmap from the previously saved point to the current point. It then sets the PictureBox's Image property to the Bitmap to display the revised image. It finishes by saving the new current point.

In the MouseUp event handler, the program sets m_Drawing to False.

 
Private m_Drawing As Boolean
Private m_LastX As Integer
Private m_LastY As Integer

Private m_Bitmap As Bitmap
Private m_Graphics As Graphics

' Allocate the first Bitmap.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    AllocateBitmap()
End Sub

' Make a new Bitmap to draw on.
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Resize
    AllocateBitmap()
End Sub

' Make a new Bitmap to draw on.
Private Sub AllocateBitmap()
    ' Make the PictureBox fill the form.
    picCanvas.SetBounds(0, 0, Me.ClientSize.Width, _
        Me.ClientSize.Height)

    ' Make a new Bitmap.
    m_Bitmap = New Bitmap(picCanvas.Width, picCanvas.Height)

    ' Make a Graphics object to draw on the Bitmap.
    m_Graphics = Graphics.FromImage(m_Bitmap)

    ' Make the PictureBox show the empty Bitmap.
    picCanvas.Image = m_Bitmap
End Sub

' Start scribbling.
Private Sub picCanvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseDown
    m_Drawing = True
    m_LastX = e.X
    m_LastY = e.Y
End Sub

' Continue scribbling.
Private Sub picCanvas_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseMove
    If m_Drawing Then
        ' Draw the new line.
        m_Graphics.DrawLine(Pens.Black, m_LastX, m_LastY, _
            e.X, e.Y)

        ' Display the result.
        picCanvas.Image = m_Bitmap

        ' Save the latest point.
        m_LastX = e.X
        m_LastY = e.Y
    End If
End Sub

' Stop scribbling.
Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    picCanvas.MouseUp
    m_Drawing = False
End Sub
 
 
Copyright © 1997-2008 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated