rohits134

Free Hand Drawing Code

Jun 30th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.22 KB | None | 0 0
  1. Imports System.Drawing
  2. Imports System.Drawing.Drawing2D
  3. Public Class form1
  4.  
  5.     Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath()
  6.  
  7.  
  8.     Dim myAlpha As Integer = 100
  9.     Dim myUserColor As New Color()
  10.     Dim myPenWidth As Single = 5
  11.  
  12.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  13.  
  14.         If e.Button = MouseButtons.Left Then
  15.  
  16.             mousePath.StartFigure()
  17.  
  18.         End If
  19.     End Sub
  20.  
  21.     Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  22.         If e.Button = MouseButtons.Left Then
  23.             Try
  24.                 mousePath.AddLine(e.X, e.Y, e.X, e.Y)
  25.  
  26.             Catch
  27.                 MsgBox("No way, Hose!")
  28.             End Try
  29.  
  30.         End If
  31.  
  32.         PictureBox1.Invalidate()
  33.     End Sub
  34.  
  35.     Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
  36.  
  37.         Try
  38.             If btnColours.Text = "Red" Then
  39.                 myUserColor = (System.Drawing.Color.Red)
  40.  
  41.                 myAlpha = 100
  42.             ElseIf btnColours.Text = "Green" Then
  43.                 myUserColor = (System.Drawing.Color.Green)
  44.  
  45.                 myAlpha = 255
  46.             ElseIf btnColours.Text = "Black" Then
  47.                 myUserColor = (System.Drawing.Color.Black)
  48.  
  49.                 myAlpha = 150
  50.             ElseIf btnColours.Text = "Blue" Then
  51.                 myUserColor = (System.Drawing.Color.Blue)
  52.  
  53.                 myAlpha = 175
  54.             ElseIf btnColours.Text = "Bisque" Then
  55.                 myUserColor = (System.Drawing.Color.Bisque)
  56.  
  57.                 myAlpha = 255
  58.             End If
  59.  
  60.             Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
  61.  
  62.             e.Graphics.DrawPath(CurrentPen, mousePath)
  63.  
  64.         Catch
  65.             ' MsgBox("Not happening!")
  66.         End Try
  67.  
  68.     End Sub
  69.  
  70.    
  71.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  72.         mousePath.Reset()
  73.         PictureBox1.Invalidate()
  74.  
  75.     End Sub
  76. End Class
Add Comment
Please, Sign In to add comment