locked
Help creating signature in picturebox RRS feed

  • Pergunta

  • Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Windows.Forms
    
    Public Class Form1
      Private xPoint As Integer
      Private yPoint As Integer
      Private bDrawing As Boolean = False
      Private bm As Bitmap
      Private gr As Graphics
      Private p As Pen
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        bm = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        p = New Drawing.Pen(Color.Black)
        gr = Graphics.FromImage(bm)
    
    
      End Sub
    
      Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    
        
        bDrawing = True
        xPoint = e.X
        yPoint = e.Y
    
        
      End Sub
    
      Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    
        If bDrawing Then
          gr.DrawLine(p, xPoint, yPoint, e.X, e.Y)
          xPoint = e.X
          yPoint = e.Y
    
          PictureBox1.Image = bm
        End If
    
        
      End Sub
    
      Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        bDrawing = False
      End Sub
    
      Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseLeave
        bDrawing = False
      End Sub
    
    End Class

    Hi guys, I need some help in creating a signature. I've done the code above. But after i draw a signature on the picturebox and when the mouse leave the picture box, this error "Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.MouseEventArgs'."

    How do you fix this error?

    Any help is greatly appreciated. Thanks.

    terça-feira, 27 de abril de 2010 20:39

Respostas

  • If i understand you correctly, you want to use mouse to write on picturebox image as signature. If that is the case, you dont have to use drawline, all you need is graphicsPath. Below is a sample code.

    Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath
    Dim myUserColor As Color
    
     Private Sub PictureBox2_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down 
    
          Try
            mousePath.AddLine(e.X, e.Y, e.X, e.Y)  'Add mouse coordiantes to mousePath
            
          Catch Ex As Exception
            
          End Try
    
        End If
    
        PictureBox2.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
    
    
      End Sub
    
     Private Sub PictureBox2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
        If e.Button = MouseButtons.Left Then
    
          mousePath.StartFigure()
          
        End If
      End Sub
    
    
      Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        Try
          myUserColor = Label2.BackColor
    
          Dim CurrentPen = New Pen(myUserColor, 2) 'Set up the pen
          
          e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
          e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path
          
        Catch ex As Exception
    
        End Try
    
      End Sub
    
    'clear
      Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        PictureBox2.CreateGraphics.Clear(PictureBox2.BackColor)
        mousePath.Reset()
      End Sub
    kaymaf

     


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marcado como Resposta Liliane Teng terça-feira, 4 de maio de 2010 09:08
    terça-feira, 27 de abril de 2010 21:23

Todas as Respostas

  • Your MouseLeave event signature is incorrect.
    terça-feira, 27 de abril de 2010 21:14
  • If i understand you correctly, you want to use mouse to write on picturebox image as signature. If that is the case, you dont have to use drawline, all you need is graphicsPath. Below is a sample code.

    Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath
    Dim myUserColor As Color
    
     Private Sub PictureBox2_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down 
    
          Try
            mousePath.AddLine(e.X, e.Y, e.X, e.Y)  'Add mouse coordiantes to mousePath
            
          Catch Ex As Exception
            
          End Try
    
        End If
    
        PictureBox2.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
    
    
      End Sub
    
     Private Sub PictureBox2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
        If e.Button = MouseButtons.Left Then
    
          mousePath.StartFigure()
          
        End If
      End Sub
    
    
      Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
        Try
          myUserColor = Label2.BackColor
    
          Dim CurrentPen = New Pen(myUserColor, 2) 'Set up the pen
          
          e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
          e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path
          
        Catch ex As Exception
    
        End Try
    
      End Sub
    
    'clear
      Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        PictureBox2.CreateGraphics.Clear(PictureBox2.BackColor)
        mousePath.Reset()
      End Sub
    kaymaf

     


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    • Marcado como Resposta Liliane Teng terça-feira, 4 de maio de 2010 09:08
    terça-feira, 27 de abril de 2010 21:23
  • Hello BlueWizzy,
    Thanks for your post.
    kaymaf has given you helpful solution. In order to facilate the forum,I marked it as answer. When you come back,you may have a try. If you think it provide no help,please unmark.Thanks.
    If you have any problems, please feel free to follow up.
    Best regards,
    Liliane
    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    terça-feira, 4 de maio de 2010 09:08
  • Thanks for this solution . how we can get this image to other picture box or save this image 

    terça-feira, 8 de outubro de 2019 05:41