|
|
Title | Make a scribble application with VB .NET that automatically redraws when necessary |
Description | |
Keywords | VB.NET, drawing, scribble, redraw, autoredraw |
Categories | Graphics, VB.NET |
|
|
When the form loads or is resized, the program calls the AllocateBitmap subroutine. This routine makes a Bitmap to fill the form, making it no smaller than it was previously. It makes a Graphics object to draw on the Bitmap and sets the PictureBox's Image property to the Bitmap to display it. (Thanks to Tom Erik Voll for this suggestion.)
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()
' Get the new desired size.
Dim wid As Integer = Me.ClientSize.Width
Dim hgt As Integer = Me.ClientSize.Width
' See if we already have a Bitmap.
If (m_Bitmap Is Nothing) Then
' Make a Bitmap.
m_Bitmap = New Bitmap(wid, hgt)
Else
' See if this is smaller than the previous size.
If wid < m_Bitmap.Width Then wid = m_Bitmap.Width
If hgt < m_Bitmap.Height Then hgt = m_Bitmap.Height
' If it's the same size as before, skip it.
If (wid = m_Bitmap.Width) And (hgt = _
m_Bitmap.Height) Then Exit Sub
' Make a new Bitmap.
Dim bm As New Bitmap(wid, hgt)
' Copy the old picture into the new one.
Using gr As Graphics = Graphics.FromImage(bm)
gr.DrawImage(m_Bitmap, 0, 0, m_Bitmap.Width, _
m_Bitmap.Height)
End Using
' Save the new picture.
m_Bitmap = bm
End If
' Make a Graphics object to draw on the Bitmap.
m_Graphics = Graphics.FromImage(m_Bitmap)
' Make the PictureBox show the 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
|
|
|
|
|
|