69304
Goto Top

Visual Basic 2008 in PictureBox zeichnen wie in Paint mit Stift

Hallo!

Für ein Projekt brauche ich eine Möglichkeit, in eine PictureBox o.ä. zeichnen zu können wie in Paint mit dem Stift-Werkzeug.

Es geht darum, dass eine Unterschrift "digitalisiert" werden kann. Ferner soll per Grafiktablett unterschrieben werden können. Das Image soll dann als BMP, etc. nachher weiter verarbeitet werden können.

Ich habe schon sowas was ich brauche im Netz gerfunden, bloß ist dies zu langsam.

Hat hier jemand eine Idee, oder Vorschlag?


Mein Code, den ich bis jetzt verwende, der aber zu langsam ist..:

Public Class frmUnterschrift

    Dim xStart, yStart, xEnd, yEnd As Integer
    Dim Drawbitmap As Bitmap
    Dim Drawgraphics As Graphics
    Dim myPen As New Pen(Color.BlueViolet, 3)
    Dim myColor As Color = Color.Black
    Dim myBrush As New Drawing.SolidBrush(Color.Black)
    Dim myBrushWidth As Integer
    Dim ContinuousFlag As Boolean

    Private Sub drawMyline()
        PictureBox1.Image = Drawbitmap
        Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Drawgraphics.DrawLine(myPen, xStart, yStart, xEnd, yEnd)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Drawbitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Drawgraphics = Graphics.FromImage(Drawbitmap)
        PictureBox1.Image = Drawbitmap
        Drawgraphics.Clear(Color.White)
        myBrushWidth = 4
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        xStart = Control.MousePosition.X - (Me.Left + PictureBox1.Left + 4)
        yStart = Control.MousePosition.Y - (Me.Top + PictureBox1.Top + 31)
        'to do continuous drawing, enable this line   
        'drawMyline()  
        If RadioButton1.Checked = True Then
            ContinuousFlag = True
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If ContinuousFlag Then
            Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            Drawgraphics.FillEllipse(myBrush, e.X, e.Y, myBrushWidth, myBrushWidth)
            PictureBox1.Image = Drawbitmap
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        xEnd = Control.MousePosition.X - (Me.Left + PictureBox1.Left + 4)
        yEnd = Control.MousePosition.Y - (Me.Top + PictureBox1.Top + 31)

        If RadioButton1.Checked Then
            ContinuousFlag = False
        Else
            drawMyline()
        End If
    End Sub

End Class

Content-Key: 135423

Url: https://administrator.de/contentid/135423

Printed on: April 19, 2024 at 11:04 o'clock

Member: MonoTone
MonoTone Feb 09, 2010 at 16:11:49 (UTC)
Goto Top
HI rbrixel

Hier mal ein C&P Code von mir aus Internet schnipseln.
Du brauchst PictureBox1, Combobox1 + 2.
Kannst ja aber selber nach deinen belieben ändern bzw siehst ja eh.

Public Class Form1
    Private MouseD As Boolean
    Private Col As Color
    Private NewPen As Pen
    Private bmp As Bitmap
    Private Plist As List(Of Point)
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        MouseD = True
        Plist.Add(New Point(e.X, e.Y))
        NewPen = New Pen(Col, CSng(ComboBox2.SelectedItem))
    End Sub
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If MouseD Then
            MouseD = False
            Using gr = Graphics.FromImage(bmp)

                Draw(gr)
                gr.Flush()
            End Using
            Plist.Clear()
            PictureBox1.Invalidate()
        End If
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If MouseD Then
            Plist.Add(New Point(e.X, e.Y))
            PictureBox1.Invalidate()
        End If
    End Sub
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If MouseD Then Draw(e.Graphics)
    End Sub
   
    Private Sub Draw(ByVal g As Graphics)
        If Plist.Count > 0 Then
            Dim bs As Byte() = New Byte(Plist.Count - 1) {}
            bs(0) = CByte(System.Drawing.Drawing2D.PathPointType.Start)
            For a = 1 To Plist.Count - 1
                bs(a) = CByte(System.Drawing.Drawing2D.PathPointType.Line)
                g.DrawPath(NewPen, New System.Drawing.Drawing2D.GraphicsPath(Plist.ToArray, bs))
            Next
        End If
    End Sub

    Private Function GetColors() As List(Of String)
        Dim colors As New List(Of String)()
        Dim colorNames As String() = [Enum].GetNames(GetType(KnownColor))
        For Each colorName As String In colorNames
            Dim knownColor As KnownColor = DirectCast([Enum].Parse(GetType(KnownColor), colorName), KnownColor)
            If knownColor > knownColor.Transparent Then
                colors.Add(colorName)
            End If
        Next
        Return colors
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.BackColor = Color.White
        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        PictureBox1.Image = bmp
        Plist = New List(Of Point)
        Me.DoubleBuffered = True
        For Each c As String In GetColors()
            ComboBox1.Items.Add(c)
        Next
        For a = 1 To 25
            ComboBox2.Items.Add(a.ToString)
        Next
        ComboBox1.SelectedItem = ComboBox1.Items(0)
        ComboBox2.SelectedItem = ComboBox2.Items(0)
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Col = Color.FromName(ComboBox1.SelectedItem)
    End Sub
   
End Class

gruss Mono
Mitglied: 69304
69304 Feb 09, 2010 at 21:00:50 (UTC)
Goto Top
Funktioniert wunderbar!!!

Danke Mono!

Habs bissl abgeändert und läuft supi!
Mitglied: 69304
69304 Feb 10, 2010 at 08:17:38 (UTC)
Goto Top
Jetzt ist mir aber gerade aufgefallen, als ich das Bild mit image.save speichern will, bekommen ich nur ein schwarzes BMP.

Hat hier jmd noch eine Lösung, dass das Bild richtig abgespeichert wird?
Mitglied: 69304
69304 Feb 15, 2010 at 23:28:03 (UTC)
Goto Top
Hallo!

Hab das Problem weiter einkreisen können ^^

Wenn ich das Image so abspeicher...

bmp.Save("D:\test_neu.bmp", Imaging.ImageFormat.Bmp)  

... bekomme ich einfach ein schwarzes Bitmap.

Wenn ich aber die Pen-Color auf Weiss und den Background auf Black stelle, bekomme ich auch ein solches Bild.

Wie könnte ich den Hintergrund weiss einfärben, weil...

PictureBox1.BackColor = Color.White

... bringts ned...
Member: MonoTone
MonoTone Feb 17, 2010 at 09:48:42 (UTC)
Goto Top
probier mal:

Dim img As Image = Image.FromHbitmap(CType(Me.PictureBox1.Image, _
              Bitmap).GetHbitmap)
img.save("C:myPic.png") 'oder als bmp/jpg mit Imaging.imageformat.bmp/jpeg etc  

bin grad auch ein bissi ratlos, so geht es aber zumindest halbwegs... ich such mal ob ich noch eine bessere lösung finde
hab ne bessere:

füge im load event ein:

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using gr as Graphics = Graphics.FromImage(bmp)
            gr.Clear(Color.White) 'das ist der Punkt  
        End Using
        PictureBox1.Image = bmp
        Plist = New List(Of Point)
        Me.DoubleBuffered = True
'..  
end sub
Private Sub Savebutton_click '..  
bmp.Save("C:\MyPic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)  
end sub


So sollte es gehen.

Gruss mono
Mitglied: 69304
69304 Feb 17, 2010 at 09:59:22 (UTC)
Goto Top
Ich habe festgestellt, dass der Background einfach "leer" ist.

Wenn ich z.B. den Hintergrund aus einem Bitmap vorlade mit...

Try
            bmp = Image.FromFile(PROGRAMMPFAD & "\bg.bmp")  
            PictureBox1.Image = bmp
            Plist = New List(Of Point)
            Me.DoubleBuffered = True
        Catch ex As Exception
            FehlerMeldung("Hintergrund fehlt! " & ex.Message, True)  

        End Try

...bekomme ich ein korrektes Image beim speichern.

Wenn ich deinen Vorlag übernehme, Monotone, dann sieht das Bild total hässlich mit einem blauen Hintergrund aus.
Mitglied: 69304
69304 Feb 17, 2010 at 10:12:05 (UTC)
Goto Top
OK, ich habs ;)

Public Function NeuesBild() As Boolean
        Try
            bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
            Using gr = Graphics.FromImage(bmp) ' Hintergrund weiss füllen  
                gr.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, PictureBox1.Height)
            End Using

            PictureBox1.Image = bmp

            Plist = New List(Of Point)
            Me.DoubleBuffered = True
            Return True
        Catch ex As Exception
            MsgBox("Hintergrund fehlt! " & ex.Message)  
            Return False
        End Try
End Function

Diese Funktion kann dann beim Form-Load ausgeführt werden!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NeuesBild()
End Sub


Danke an Monotone!