Results 1 to 9 of 9

Thread: Saving a drawn signature as a bitmap

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Saving a drawn signature as a bitmap

    Hello,

    I am still confused about a few things.

    Firstly my code for saving the signature that has been captured on the picturebox1.

    vb Code:
    1. Code SnippetPrivate Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
    2.  
    3. Try
    4.  
    5. Dim bmp As New Bitmap(Me.PictureBox1.Image)
    6.  
    7. Using g As Graphics = Graphics.FromImage(bmp)
    8.  
    9. Using pen As Pen = New Pen(Color.Red, 2)
    10.  
    11. For Each pointList As List(Of Point) In strokeCollection
    12.  
    13. If (pointList.Count >= 2) Then
    14.  
    15. g.DrawLines(pen, pointList.ToArray())
    16.  
    17. End If
    18.  
    19. Next pointList
    20.  
    21. End Using
    22.  
    23. End Using
    24.  
    25. Catch ex As Exception
    26.  
    27. MsgBox(ex.Message)
    28.  
    29. End Try
    30.  
    31. End Sub

    Here I get an error message on this line: Dim bmp As New Bitmap(Me.PictureBox1.Image). 'The picture.Image is NULL'

    My understanding that the picturebox1.Image should be the signature that has been captured on the pictureBox1. And as this has been drawn over, it shouldn't really be null but contain the signature itself. Correct me if I am wrong?

    Many thanks for your help.

    The rest of the code for my program is below if you need to review.

    vb Code:
    1. Code Snippet
    2.  
    3. #Region "Local Variables"
    4.  
    5. Private lastPoint As Point
    6.  
    7. Private strokeCollection As New List(Of List(Of Point))
    8.  
    9. Private currentStroke As New List(Of Point)
    10.  
    11. #End Region
    12.  
    13. #Region "Constructor"
    14.  
    15. Public Sub New()
    16.  
    17. InitializeComponent()
    18.  
    19. AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint
    20.  
    21. End Sub
    22.  
    23. #End Region
    24.  
    25. #Region "PictureBox1_Paint Event"
    26.  
    27. Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As PaintEventArgs)
    28.  
    29. For Each pointList As List(Of Point) In strokeCollection
    30.  
    31. If pointList.Count >= 2 Then
    32.  
    33. Using pen As New Pen(Color.Black, 2)
    34.  
    35. e.Graphics.DrawLines(pen, pointList.ToArray())
    36.  
    37. End Using
    38.  
    39. End If
    40.  
    41. Next
    42.  
    43. End Sub
    44.  
    45. #End Region
    46.  
    47. #Region "Picture box mouse down event"
    48.  
    49. Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    50.  
    51. Try
    52.  
    53. Me.lastPoint = New Point(e.X, e.Y)
    54.  
    55. Me.currentStroke = New List(Of Point)
    56.  
    57. Me.strokeCollection.Add(Me.currentStroke)
    58.  
    59. Catch ex As Exception
    60.  
    61. MsgBox(ex.Message)
    62.  
    63. End Try
    64.  
    65. End Sub
    66.  
    67. #End Region
    68.  
    69. #Region "Picture box mouse move event"
    70.  
    71. Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    72.  
    73. Dim hDC As IntPtr = GetDC(Me.PictureBox1.Handle)
    74.  
    75. Using g As Graphics = Graphics.FromHdc(hDC)
    76.  
    77. Using pen As Pen = New Pen(Color.Black, 2)
    78.  
    79. g.DrawLine(pen, lastPoint.X, lastPoint.Y, e.X, e.Y)
    80.  
    81. End Using
    82.  
    83. End Using
    84.  
    85. ReleaseDC(Me.PictureBox1.Handle, hDC)
    86.  
    87. Me.lastPoint = New Point(e.X, e.Y)
    88.  
    89. Me.currentStroke.Add(Me.lastPoint)
    90.  
    91. End Sub
    92.  
    93. #End Region
    94.  
    95. #Region "Attributes"
    96.  
    97. <System.Runtime.InteropServices.DllImport("coredll.dll")> _
    98.  
    99. Private Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
    100.  
    101. End Function
    102.  
    103. <System.Runtime.InteropServices.DllImport("coredll.dll")> _
    104.  
    105. Private Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
    106.  
    107. End Function
    108.  
    109. #End Region
    Last edited by steve_rm; Sep 18th, 2007 at 12:16 PM.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width