Public Class Form1
Enum ButtonToggle
Up = 1
Down = 2
End Enum
Enum DrawingBrush
FreeStyle = 0
DrawBox = 1
DrawEllips = 2
End Enum
Dim SignatureFile As String = "C:\signature.bmp"
Dim ButtonState As ButtonToggle = ButtonToggle.Up
Dim mouseCoord As New Collection
Dim grp As New Drawing2D.GraphicsPath
Dim DrawSurface As Graphics
Dim LastDraw As PictureBox
Dim DrawBMP As Bitmap
Dim CurrentDrawBrush As DrawingBrush = DrawingBrush.FreeStyle
Dim DashPen As System.Drawing.Pen
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
NewDrawSurface()
DashPen = New System.Drawing.Pen(DrawColor_btn.BackColor)
DashPen.DashStyle = Drawing2D.DashStyle.Dash
End Sub
'Create a new surface to draw on
Private Sub NewDrawSurface()
'Time stamp the filename
SignatureFile = SignatureFile.Replace("signature", "signature " & Now.ToString("MM-dd-yy hhmmss"))
DrawBMP = New Bitmap(Signature_pb.Width, Signature_pb.Height)
DrawSurface = Graphics.FromImage(DrawBMP)
DrawSurface.FillRectangle(New SolidBrush(Color.Gray), 0, 0, DrawBMP.Width, DrawBMP.Height)
End Sub
Private Sub Signature_pb_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseMove
If ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.FreeStyle Then
mouseCoord.Add(e.Location)
If mouseCoord.Count > 1 Then
grp.AddLine(mouseCoord.Item(mouseCoord.Count - 1), e.Location)
Signature_pb.CreateGraphics.DrawPath(New Pen(DrawColor_btn.BackColor), grp)
DrawSurface.DrawPath(New Pen(DrawColor_btn.BackColor), grp)
End If
ElseIf ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.DrawBox Then
Signature_pb = LastDraw
Signature_pb.Refresh()
mouseCoord.Add(e.Location)
Dim topPoint As System.Drawing.Point = mouseCoord(1)
Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)
Dim ngrp As New Drawing2D.GraphicsPath
ngrp.AddRectangle(boxRec)
Signature_pb.CreateGraphics.DrawPath(DashPen, ngrp)
ElseIf ButtonState = ButtonToggle.Down And CurrentDrawBrush = DrawingBrush.DrawEllips Then
Signature_pb = LastDraw
Signature_pb.Refresh()
mouseCoord.Add(e.Location)
Dim topPoint As System.Drawing.Point = mouseCoord(1)
Dim ellipsRec As Rectangle = CalculateRectangle(topPoint, e.Location)
Dim ngrp As New Drawing2D.GraphicsPath
ngrp.AddEllipse(ellipsRec)
Signature_pb.CreateGraphics.DrawPath(DashPen, ngrp)
End If
'x_lbl.Text = e.X
'Y_lbl.Text = e.Y
'If mouseCoord.Count > 0 Then lastCoord_lbl.Text = mouseCoord(1).ToString
End Sub
Private Sub Signature_pb_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseDown
If ButtonState = ButtonToggle.Up Then LastDraw = Signature_pb
ButtonState = ButtonToggle.Down
End Sub
Private Sub Signature_pb_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Signature_pb.MouseUp
If mouseCoord.Count = 0 And CurrentDrawBrush = DrawingBrush.FreeStyle Then
'Must dot our i's
Dim bmp As New Bitmap(1, 1)
bmp.SetPixel(0, 0, Color.White)
Signature_pb.CreateGraphics.DrawImage(bmp, e.Location)
DrawSurface.DrawImage(bmp, e.Location)
ElseIf CurrentDrawBrush = DrawingBrush.DrawBox And mouseCoord.Count > 0 Then
Dim topPoint As System.Drawing.Point = mouseCoord(1)
Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)
DrawSurface.DrawRectangle(New Pen(DrawColor_btn.BackColor), boxRec)
'ready for new what ever.
mouseCoord.Clear()
grp = New Drawing2D.GraphicsPath
ElseIf CurrentDrawBrush = DrawingBrush.DrawEllips And mouseCoord.Count > 0 Then
Dim topPoint As System.Drawing.Point = mouseCoord(1)
Dim boxRec As Rectangle = CalculateRectangle(topPoint, e.Location)
DrawSurface.DrawEllipse(New Pen(DrawColor_btn.BackColor), boxRec)
'ready for new what ever.
mouseCoord.Clear()
grp = New Drawing2D.GraphicsPath
Else
'ready for new word
mouseCoord.Clear()
grp = New Drawing2D.GraphicsPath
End If
Signature_pb.Image = DrawBMP
ButtonState = ButtonToggle.Up
End Sub
Private Function CalculateRectangle(ByVal p1 As System.Drawing.Point, ByVal p2 As System.Drawing.Point) As System.Drawing.Rectangle
Dim TopX As Integer
Dim TopY As Integer
Dim xBig As Integer = Math.Max(p1.X, p2.X)
Dim xSmall As Integer = Math.Min(p1.X, p2.X)
Dim yBig As Integer = Math.Max(p1.Y, p2.Y)
Dim ySmall As Integer = Math.Min(p1.Y, p2.Y)
Dim recWidth As Integer = xBig - xSmall
Dim recHeight As Integer = yBig - ySmall
If p1.X > p2.X Then
TopX = p2.X
Else
TopX = p1.X
End If
If p1.Y > p2.Y Then
TopY = p2.Y
Else
TopY = p1.Y
End If
Return New Rectangle(TopX, TopY, recWidth, recHeight)
End Function
Private Sub Clear_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_btn.Click
Signature_pb.CreateGraphics.Clear(Signature_pb.BackColor)
NewDrawSurface()
mouseCoord.Clear()
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
PictureBox1.Image = Signature_pb.Image
Signature_pb.Image.Save(SignatureFile)
End Sub
Private Sub free_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles free_btn.Click
CurrentDrawBrush = DrawingBrush.FreeStyle
End Sub
Private Sub box_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles box_btn.Click
CurrentDrawBrush = DrawingBrush.DrawBox
End Sub
Private Sub ellips_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ellips_btn.Click
CurrentDrawBrush = DrawingBrush.DrawEllips
End Sub
Private Sub DrawBtns_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles free_btn.CheckedChanged, box_btn.CheckedChanged, ellips_btn.CheckedChanged
Dim currentButton As RadioButton = CType(sender, RadioButton)
If currentButton.Checked = True Then
currentButton.BackColor = System.Drawing.SystemColors.ButtonShadow
Else
currentButton.BackColor = System.Drawing.SystemColors.ButtonFace
End If
End Sub
Private Sub DrawColor_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawColor_btn.Click
Dim cldlg As New ColorDialog
If cldlg.ShowDialog = Windows.Forms.DialogResult.OK Then
DrawColor_btn.BackColor = cldlg.Color
End If
End Sub
End Class