
69304
08.02.2010
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..:
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..:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 135423
Url: https://administrator.de/forum/visual-basic-2008-in-picturebox-zeichnen-wie-in-paint-mit-stift-135423.html
Ausgedruckt am: 12.04.2025 um 18:04 Uhr
7 Kommentare
Neuester Kommentar
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.
gruss Mono
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
probier mal:
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:
So sollte es gehen.
Gruss mono
1
2
3
2
3
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
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