Vb.net 2 Signatures - vb.net

Im quite finished with my program, there are only 2 more things to do. But now lets focus on my main problem: Ive got a code, that gives the user the possibility to draw inside a picturebox, that will work later as a signature. Everything was working fine until I added a second picturebox with the same code( yes I changed for ex. picturebox1 to pixturebox2). As I start drawing I cant let go and both pictureboxes are getting interracted at the same time.
That my code for both pictureboxes.
Private _Previous As System.Nullable(Of Point) = Nothing
Private Sub pictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
_Previous = e.Location
pictureBox1_MouseMove(sender, e)
End Sub
Private Sub pictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If _Previous IsNot Nothing Then
If PictureBox1.Image Is Nothing Then
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Using g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.White)
End Using
PictureBox1.Image = bmp
End If
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.DrawLine(Pens.Black, _Previous.Value, e.Location)
End Using
PictureBox1.Invalidate()
_Previous = e.Location
End If
End Sub
Private Sub pictureBox2_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox2.MouseDown
_Previous = e.Location
pictureBox1_MouseMove(sender, e)
End Sub
Private Sub pictureBox2_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox2.MouseMove
If _Previous IsNot Nothing Then
If PictureBox2.Image Is Nothing Then
Dim bmp As New Bitmap(PictureBox2.Width, PictureBox2.Height)
Using g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.White)
End Using
PictureBox2.Image = bmp
End If
Using g As Graphics = Graphics.FromImage(PictureBox2.Image)
g.DrawLine(Pens.Black, _Previous.Value, e.Location)
End Using
PictureBox2.Invalidate()
_Previous = e.Location
End If
End Sub
Private Sub pictureBox2_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox2.MouseUp
_Previous = Nothing
End Sub
I know there is an mistake somewere, but Im to excited to see it.

In this event handler for PictureBox2 you call the handler for PictureBox1:
Private Sub pictureBox2_MouseDown(ByVal sender As Object, _
ByVal e As MouseEventArgs) Handles PictureBox2.MouseDown
_Previous = e.Location
pictureBox1_MouseMove(sender, e)
End Sub
I'm guessing changing it to:
pictureBox2_MouseMove(sender, e)
will fix it.

Related

draw a few lines on an image with mouse move

I want to draw a few lines on an image
Using mouse move in vb.net.
My problem is that when I start drawing the next line, the previous line disappears!
Can anyone help me?
I put my code along with a photo of run my project
Dim st, en As New Point
Dim p As Pen
Private mouseButtonPressed As Boolean = False
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
p = New Pen(Color.Black, 2)
Dim g As Graphics = e.Graphics
g.DrawLine(p, st, en)
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
mouseButtonPressed = False
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If mouseButtonPressed Then
PictureBox1.Invalidate()
en = e.Location
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
st = New Point(e.X, e.Y)
mouseButtonPressed = True
End If
End Sub
You can store the lines in a list as they get completed, then draw all the previous lines when you are drawing the live one
Private st, en As New Point
Private mouseButtonPressed As Boolean = False
Private lines As New List(Of (st As Point, en As Point))
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
Dim p = New Pen(Color.Black, 2)
Dim g As Graphics = e.Graphics
g.DrawLine(p, st, en)
For Each line In lines
g.DrawLine(p, line.st, line.en)
Next
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
mouseButtonPressed = False
lines.Add((st, en))
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If mouseButtonPressed Then
PictureBox1.Invalidate()
en = e.Location
End If
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
st = New Point(e.X, e.Y)
mouseButtonPressed = True
End If
End Sub
The list holds Tuple(of Point, Point)

MouseEventArgs from a subroutine

I have a chart in Visual Studio 2013 and I can draw a line on it using the MouseventArgs handler. No worries.
But.... how can you have a separate subroutine that then uses the mouse events to draw a line. What I am need is if I hit a button, draw a line, if I did not hit the button use the regular mouseventargs.
So the button needs to start this:
enter code herePrivate Sub Chart1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseDown
FirstPoint = New Point(e.X, e.Y)
TempPoint = New Point(e.X, e.Y)
End Sub
Private Sub Chart1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseMove
If Not FirstPoint = Nothing Then
Dim g As Graphics = chD.CreateGraphics
Dim ErasePen As Pen = New Pen(Me.BackColor)
g.DrawLine(ErasePen, FirstPoint, TempPoint)
TempPoint = New Point(e.X, e.Y)
Me.Refresh()
End If
End Sub
Private Sub Chart1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles chD.MouseUp
Dim Line As New LineEndPoints With {.StartPoint = FirstPoint, .endPont = New Point(e.X, e.Y)}
LinesList.Add(Line)
FirstPoint = Nothing
Me.Refresh()
End Sub
Private Sub Chart1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles chD.Paint
For Each line As LineEndPoints In LinesList
e.Graphics.DrawLine(Pens.Yellow, line.StartPoint, line.endPont)
Next
If Not FirstPoint = Nothing Then
e.Graphics.DrawLine(Pens.Yellow, FirstPoint, TempPoint)
End If
End Sub`
`
Does anyone have any idea how I can achieve this?

How to paint on Picturebox VB.net

I have start a new project and i search for some solution to paint on a picturebox
with this code i am able to draw on a form but the i need to draw on a picture box, i have try multiple ways but i can not find the way to do it on the screenshoot in the picturebox
What i need to change to get it work?
This is my code
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor = Cursors.Hand
End Sub
Dim mustPaint As Boolean = False
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
mustPaint = True
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
mustPaint = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
PictureBox1.BackgroundImage = screenshot
End Sub
End Class
Do you have rights to write directly to c:\? What error message do you get?
Maybe try without saving the image file
'screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
This absolutely puts a screenshot on the PictureBox. I don't know what else to tell you
PictureBox1.Image = screenshot
Here, I clicked the button 6 times and it kept working!
Well after to try so many times i got it work
This is the working code
Imports System.Drawing.Drawing2D
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Dim mustPaint As Boolean = False
Private lastPT As Point
Private signature As New GraphicsPath
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
lastPT = New Point(e.X, e.Y)
End If
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim curPt As New Point(e.X, e.Y)
signature.AddLine(lastPT, curPt)
lastPT = curPt
PictureBox1.Refresh()
End If
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
signature.StartFigure()
End If
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If Not IsNothing(signature) Then
e.Graphics.DrawPath(Pens.Black, signature)
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
signature.Reset()
PictureBox1.Refresh()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim bmp As New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
bmp.Save(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "test.bmp"), System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
mustPaint = True
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
mustPaint = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
'PictureBox1.BackgroundImage = screenshot
PictureBox1.Image = screenshot
End Sub
End Class

How to move picture boxes in a panel using vb.net

I'm trying to move picture boxes in a panel.
This is my Code:
Private dragging As Boolean
Private beginX, beginY As Integer
Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = True
beginX = CType(sender, PictureBox).Location.X
beginY = CType(sender, PictureBox).Location.Y
End Sub
Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim cntrl As Control = CType(sender, Control)
If dragging = True Then
cntrl.Location = New Point(cntrl.Location.X + e.X - beginX, cntrl.Location.Y + e.Y - beginY)
'Me.Refresh()
End If
End Sub
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
dragging = False
End Sub
I can't figure out why this don't work.
The subroutines you have are missing their handlers (ie the handles statement) at the end.
ex:
Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) HANDLES controlName.MouseUp
dragging = False
End Sub
Try this:
Dim cmd As Boolean = False
Dim sp As Point
Private Sub Form1_Load() Handles MyBase.Load
For Each Control As Picturebox In Me.Controls.OfType(Of Picturebox)
AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
cmd = True
sp = e.Location
End Sub
AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
If cmd Then
Control.Location = Control.Location - sp + e.Location
End If
End Sub
AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
cmd = False
End Sub
Next
End Sub

Can't draw rectangle around textbox in Groupbox, using Visual Basic 2010.-

I have two textbox controls in a vb form.- I can draw rectangles about the two textbox if they aren't in a groupbox.- Only when the textbox got the focus the rectangle is drawed.- The code works, but somehow, when i put the textbox in a groupbox the rectangles aren't drawed.-
the rectangle only is drawed when textbox is focused
This is the code that i have been using.-
Public Class Form1
Dim curControl As TextBox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
If Not curControl Is Nothing Then
If curControl.Name = "TextBox1" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Lime, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox1.Location, TextBox1.Size))
pen.Dispose()
End If
End If
If Not curControl Is Nothing Then
If curControl.Name = "TextBox2" Then
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Red, 2.0)
g.DrawRectangle(pen, New Rectangle(TextBox2.Location, TextBox2.Size))
pen.Dispose()
End If
End If
End Sub
Private Sub TextBox_Enter(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
curControl = DirectCast(sender, TextBox)
Me.Invalidate()
End Sub
Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave
curControl = Nothing
Me.Invalidate()
End Sub