• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

Drawing lines and saving as bitmap

I'm getting a signature in a PDA. Basically, I'm drawing points as the user writes their name. I'm saving those points in a text file in a database. At a web site, I want to have a web service use a function to redraw the signature and save it as a bitmap. However, I can't seem to get a graphics object to play with the way I think I'm supposed to. For example, I can
Dim PN as new Panel

But there is no pn.Graphics object like there is on the PDA. Anyways, I'm not very experienced with graphics and I need some code to show me how I can create a something (with code only) that will allow me to redraw the points that make the signature. Once I have the signature redrawn, I want to create a bitmap that I can save somewhere.
Here is some code I use in the PDA:
Private Sub FillSignature(ByVal Signature As String)
        Dim PlotX, PlotY, PlotXold, PlotYold, SigPos As Long
            'Dim file As New System.IO.StreamReader("\windows\mysig.txt")
            SigData = Signature 'file.ReadToEnd()
            SigPos = 1
            While SigPos < Len(SigData)
                PlotX = Val("&H" & Mid(SigData, SigPos, 2))
                PlotY = Val("&H" & Mid(SigData, SigPos + 2, 2))
                If PlotY > 127 Then
                    PlotXold = PlotX
                    PlotYold = PlotY - 128
                    Me.graphics.DrawLine(blackPen, PlotXold, PlotYold, PlotX, PlotY)
                    Me.pDrawWindow.Invalidate(New Rectangle(Math.Min(PlotX, PlotXold), Math.Min(PlotY, PlotYold), Math.Abs(PlotXold - PlotX) + 1, Math.Abs(PlotYold - PlotY) + 1))
                    PlotXold = PlotX
                    PlotYold = PlotY
                End If
                SigPos = SigPos + 4
            End While
        Catch ex As Exception
            MsgBox("Error writing signature", MsgBoxStyle.Information, "Signature error...")
            Exit Sub
        End Try
    End Sub

Open in new window

  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
Some high level code on how to draw to a bitmap in VB.Net:

        Dim bmpFileName As String = "c:\someImage.bmp"
        Dim bmp As New Bitmap(yourWidthHere, yourHeightHere)
        Dim G As Graphics = Graphics.FromImage(bmp)

        ' ...now draw with "G"...

        ' some kind of loop here:
            G.DrawLine(Pens.Black, pointA, pointB)

Mike TomlinsonMiddle School Assistant TeacherCommented:
Oh...you can paint the background of the bitmap to a specified color with:

Hello, Misledman,

Idle_Mind's suggestions are right on for creating the bitmap that you want to save.

If you still want to draw the signature on the Panel, just use the panel's Paint event.  It provides a PaintEventArgs parameter (e) that contains the graphics (e.Graphics) on which you would draw your connected lines.

But I wonder why you would bother to save the bitmap, when you already have all the information in the "points" text file.  This will probably be a more compact file than the bitmap, and whenever you need a bitmap to display to a user..., well you already have all of the code required to recreate it on demand.


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now