Visual Basic GDI+ Drawing Lines - vb.net

I am working on a simple VB interface that should allow the user to draw a line from the point the mouse is clicked down to whereever the mouse position is, then leave the line where the mouse button is released. I am new to VB GDI+ and could use some pointers or tips. I need it so that I can draw multiple lines on the same control panel and everytime I start a new line somehow make it that the Paint paint function does not have to loop through each line I already drew. Here is my Code:
Public Class Form1
Dim down = False
Dim ptA As Point
Dim ptB As Point
Dim ptC As Point
Dim ptStart As New List(Of Point) ' Starting point of each line
Dim ptEnd As New List(Of Point) ' Ending Point of Each line
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
End Sub
Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
down = True
ptA = New Point(e.X, e.Y)
End Sub
Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
If down = True Then
ptB = New Point(e.X, e.Y)
Win.Invalidate()
End If
End Sub
Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
down = False
ptC = New Point(e.X, e.Y)
ptStart.Add(ptA)
ptEnd.Add(ptC)
End Sub
Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
e.Graphics.Clear(Color.Black)
e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
For i = 0 To ptStart.Count - 1
Win.CreateGraphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
Next
End Sub
End Class
In order to keep my existing lines, I stored the points in a list and each time I move the mouse while drawing, I call the Paint function and each time it is called, it loops through each point and draws a line between them. Any suggestions would be greatly appreciated.
Thank you,
JD
** Edit
Public Class Form1
Dim down = False
Dim ptA As Point
Dim ptB As Point
Dim ptC As Point
Dim ptStart As New List(Of Point)
Dim ptEnd As New List(Of Point)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DoubleBuffered = True
End Sub
Private Sub Win_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseDown
down = True
ptA = New Point(e.X, e.Y)
End Sub
Private Sub Win_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseMove
If down = True Then
ptB = New Point(e.X, e.Y)
Win.Invalidate()
End If
End Sub
Private Sub Win_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Win.MouseUp
down = False
ptC = New Point(e.X, e.Y)
ptStart.Add(ptA)
ptEnd.Add(ptC)
End Sub
Private Sub Win_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Win.Paint
e.Graphics.Clear(Color.Black)
e.Graphics.DrawLine(Pens.LawnGreen, ptA, ptB)
For i = 0 To ptStart.Count - 1
e.Graphics.DrawLine(Pens.LawnGreen, ptStart(i), ptEnd(i))
Next
End Sub
End Class

Related

Display text for drag and drop images VB.NET

I am developing a simple SQL generator for images. I am having issues getting texts to be displayed in a textbox when I drag pictures into a PictureBox. Am I doing anything wrong? I want a situation when I drag the image into the PictureBox, the textbox shown in blue should display: 'SELECT FROM EMPLOYEE;'. I need help to get this code working. My code is displayed below.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'' picDropTarget.AllowDrop = True
picAccept.AllowDrop = True
End Sub
Private Sub picSELECT_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picSELECT.MouseDown, picEMPLOYEE.MouseDown
' Start the drag if it's the left mouse button.
If (e.Button = MouseButtons.Left) Then
Dim source As PictureBox = DirectCast(sender, PictureBox)
picSELECT.DoDragDrop(source.Image, DragDropEffects.Copy)
'Added this line to show
'txtSQL.Text = "SELECT"
End If
End Sub
Private Sub picAccept_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragEnter
' See if this is a copy and the data includes an image.
If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso
(e.AllowedEffect And DragDropEffects.Copy) <> 0) _
Then
' Allow this.
'e.Effect = DragDropEffects.All
e.Effect = DragDropEffects.All
Else
' Don't allow any other drop.
' e.Effect = DragDropEffects.None
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub picAccept_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles picAccept.DragDrop, picSELECT.DragDrop, picEMPLOYEE.DragDrop
Dim bm As Bitmap = DirectCast(e.Data.GetData(DataFormats.Bitmap, True), Bitmap)
picAccept.Image = bm
End SubEnd Class
TextBox1.Text = "select from EMPLOYEES"
Here's a fully working minimal sample. You can copy/paste the below, drop a file into the PictureBox at the bottom of the form, and the TextBox at the top will get populated with text.
Public Class Form1
Dim txtbx As New TextBox
Dim pctbx As New PictureBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtbx.Dock = DockStyle.Top
pctbx.Dock = DockStyle.Bottom
pctbx.BackColor = Color.AntiqueWhite
pctbx.AllowDrop = True
Controls.Add(txtbx)
Controls.Add(pctbx)
AddHandler pctbx.DragEnter, AddressOf pctbx_DragEnter
AddHandler pctbx.DragDrop, AddressOf pctbx_DragDrop
End Sub
Private Sub pctbx_DragEnter(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.All
End Sub
Private Sub pctbx_DragDrop(sender As Object, e As DragEventArgs)
txtbx.Text = "select from employees where id = "
End Sub
End Class

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

Moving through pictures with picturebox and next previous buttons

I'm beginner level learner in using VB and currently I am trying create two buttons that will allow me to slide through pictures in an online folder. I need some kind of a way to change the image number in the URL so that the buttons will allow me to move through the pictures.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=1&key=xxxxx")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img=2&key=xxxxxxx")
Try this, notice the private variable.
Private _CurrentImage as Int32 = 1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.load(string.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage ))
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
CurrentImage += 1
Dim pb1 As dispImg
pb1 = New dispImg()
pb1.picBox = PictureBox10
pb1.loadstring.format("http://aipot.wowspace.org/imageapix.php?uid=iti2015&folderid=71&img={0}&key=xxxxx",_CurrentImage )))
End Sub
I am assuming Button4 is PREVIOUS and Button5 is NEXT. You will want to do some work to prevent numbers out of range but you can see how easy it is to Increment/Decrement a number and then stuff it in a string.

How to make a picturebox hide when dragged over another picturebox?

So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.