# How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Page 1 of 1

## 14 Replies - 1411 Views - Last Post: 07 August 2015 - 08:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=379684&amp;s=c2beb5a7353f47b66f28fad116c30327&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

# How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 03 August 2015 - 06:22 AM

Hello
I want to know line position (X1,Y1,X2,Y2) when I click mouse on Picturebox?
Thank you.

This post has been edited by little_prince41: 03 August 2015 - 06:31 AM

Is This A Good Question/Topic? 0

## Replies To: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

• Home Owner

Reputation: 571
• Posts: 2,647
• Joined: 11-May 12

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 03 August 2015 - 06:28 AM

I believe there is a Screen.PointToClient in .net that can give you coordinates. You could also get the position of your mouse, and the position/dimensions of the picturebox, and do some simple math to figure out exactly where on the picture box the user clicked.

### #3 dday9

• D.I.C Regular

Reputation: 95
• Posts: 495
• Joined: 17-April 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 03 August 2015 - 09:03 AM

If you generate the PictureBox control's MouseDown event, then you can use a conditional statement to check if the click was a left-click and then use the MouseEventArgs to get the Location. Here is a quick example:
```Private Sub MyPictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyPictureBox.MouseDown
If e.Button = MouseButtons.Left Then
Console.WriteLine(e.Location.ToString())
End If
End Sub
```

Edit - I forgot to include the conditional statement, so I added it to the code.

Edit #2 - I realized that you're trying to create a program that will draw lines from where the user clicks. Try doing something along these lines:
```Private pts As List(Of Point) = New List(Of Point)
Private Sub MyPictureBox_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyPictureBox.MouseDown
If e.Button = MouseButtons.Left Then
End If
End Sub

Private Sub MyPictureBox_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyPictureBox.Paint
Using linePen As Pen = New Pen(Color.Black)
e.Graphics.DrawLines(linePen, pts.ToArray())
End Using
End Sub
```

This post has been edited by dday9: 03 August 2015 - 09:07 AM

### #4 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 03 August 2015 - 09:05 AM

I'm trying with this code but it's not work yet.
```Public Class Form1
Dim X() As Integer = {50, -50}
Dim Y() As Integer = {50, 50}
Dim Xp As Integer = 300 'Start at center of picturebox
Dim Yp As Integer = 300 'Start at center of picturebox
Dim Xm As Integer = 0
Dim Ym As Integer = 0
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
For i As Integer = 0 To X.Length - 1
If (Xm / Ym) = ((Xp + X(i)) / (Yp + Y(i))) Then
e.Graphics.DrawLine(Pens.Yellow, Xp, Yp, X(i), Y(i))
Else
e.Graphics.DrawLine(Pens.Blue, Xp, Yp, X(i), Y(i))
End If
Xp += X(i)
Yp += Y(i)
Next

End Sub

Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
Xm = e.X 'Mouse x postion
Ym = e.Y 'Mouse Y postion
Xp = 300
Yp = 300
Me.PictureBox1.Refresh()
End Sub
End Class
```

### #5 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 04 August 2015 - 09:56 AM

I found the solution for solve my problem as this link, Thanks, Right now it's work
This is my modify code.**Maybe it can help someone who it has a problem like me.
```Public Class Form1
Dim X() As Integer = {100, 100}
Dim Y() As Integer = {50, 200}
Dim Xp As Integer = 200 'Start at center of picturebox
Dim Yp As Integer = 200 'Start at center of picturebox
Dim point1() As Double = New Double() {0, 0}
Dim point2() As Double = New Double() {0, 0}
Dim pointToCheck() As Double = New Double() {0, 0}
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
For i As Integer = 0 To X.Length - 1
point1(0) = Xp
point1(1) = Yp
point2(0) = Xp + X(i)
point2(1) = Yp + Y(i)

Dim similarityRatio As Double = 0.9
Dim minValSimilarDistance As Double = 0.001
Dim similarityDistance As Double = 0.5

Dim eq1 As Double = (point2(0) - point1(0)) * (pointToCheck(1) - point1(1))
Dim eq2 As Double = (point2(1) - point1(1)) * (pointToCheck(0) - point1(0))
Dim maxVal As Double = eq1
If (eq2 > eq1) Then maxVal = eq2
Dim inLine = False
Dim isInBetween As Boolean = False

If (eq1 = eq2 OrElse (maxVal > 0 AndAlso Math.Abs(eq1 - eq2) / maxVal <= (1 - similarityRatio))) Then
inLine = True
ElseIf (eq1 <= minValSimilarDistance AndAlso eq2 <= similarityDistance) Then
inLine = True
ElseIf (eq2 <= minValSimilarDistance AndAlso eq1 <= similarityDistance) Then
inLine = True
End If

If (inLine) Then
Dim insideX As Boolean = False
If (pointToCheck(0) >= point1(0) AndAlso pointToCheck(0) <= point2(0)) Then
insideX = True
ElseIf (pointToCheck(0) >= point2(0) AndAlso pointToCheck(0) <= point1(0)) Then
insideX = True
End If
If (insideX) Then
If (pointToCheck(1) >= point1(1) AndAlso pointToCheck(1) <= point2(1)) Then
isInBetween = True
ElseIf (pointToCheck(1) >= point2(1) AndAlso pointToCheck(1) <= point1(1)) Then
isInBetween = True
End If
End If
End If

If (isInBetween) Then
'Draw new line for highlight
e.Graphics.DrawLine(Pens.Yellow, Xp, Yp, Xp + X(i), Yp + Y(i))
Else
'Draw ogirinal line
e.Graphics.DrawLine(Pens.Blue, Xp, Yp, Xp + X(i), Yp + Y(i))
End If
Xp += X(i)
Yp += Y(i)
Next
End Sub

Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
Xp = 200
Yp = 200
pointToCheck(0) = e.X 'Get X postion
pointToCheck(1) = e.Y 'Get Y postion
Me.PictureBox1.Refresh()
End Sub

End Class

```

This post has been edited by little_prince41: 04 August 2015 - 10:02 AM

### #6 dday9

• D.I.C Regular

Reputation: 95
• Posts: 495
• Joined: 17-April 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 04 August 2015 - 10:40 AM

Quote

I found the solution for solve my problem as this link, Thanks, Right now it's work
This is my modify code.**Maybe it can help someone who it has a problem like me.

What happens when you debug my code?

### #7 IronRazer

• Custom Control Freak

Reputation: 1530
• Posts: 3,856
• Joined: 01-February 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 04 August 2015 - 05:12 PM

Glad to see you got it working. 8)

I figured i would also mention that you could use GraphicsPaths to do this also. I did not study your code to see exactly what you are doing but, you can add the lines to GraphicsPaths and keep a List of each GraphicsPath.

Then you can use the GraphicsPath.IsOutlineVisible Method to get the Line/GraphicsPath that is clicked on with the mouse.

Here is an example of doing this.
```Imports System.Drawing.Drawing2D

Public Class Form1
Private gpList As New List(Of GraphicsPath)
Private SelectedGp As GraphicsPath
Private gpPen As New Pen(Color.Blue, 2)
Private StartPoint As New Point(100, 100)
Private DataPoints() As Point = {New Point(100, 50), New Point(100, 200)}

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Be sure to Dispose all the GraphicsPaths and the Pen when the form is closing
For i As Integer = gpList.Count - 1 To 0 Step -1
gpList(i).Dispose()
gpList.RemoveAt(i)
Next
gpPen.Dispose()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add each line to a new GraphicsPath and add it to the List of GraphicsPaths
For i As Integer = 0 To DataPoints.Length - 1
Dim gp As New GraphicsPath
Dim pnt2 As New Point(StartPoint.X + DataPoints(i).X, StartPoint.Y + DataPoints(i).Y)
StartPoint = pnt2
Next
End Sub

Public Sub PictureBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'Paint each Line/GraphicsPath in the List of GraphicsPaths
For Each gp As GraphicsPath In gpList
gpPen.Color = Color.Blue 'make sure the pen is set to the default color Blue
If gp Is SelectedGp Then gpPen.Color = Color.Gold 'if the current path is the selected path then set the pen color to Gold
e.Graphics.DrawPath(gpPen, gp)
Next
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
SelectedGp = Nothing
Using grx As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
For Each gp As GraphicsPath In gpList
If gp.IsOutlineVisible(e.Location, gpPen, grx) Then
SelectedGp = gp
Me.Text = "(" & gp.PathPoints(0).X.ToString & ", " & gp.PathPoints(0).Y.ToString & ")  (" & gp.PathPoints(1).X.ToString & ", " & gp.PathPoints(1).Y.ToString & ")"
Exit For
End If
Next
End Using
PictureBox1.Refresh()
End Sub

End Class

```

### #8 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 04 August 2015 - 05:26 PM

dday9, on 04 August 2015 - 10:40 AM, said:

Quote

I found the solution for solve my problem as this link, Thanks, Right now it's work
This is my modify code.**Maybe it can help someone who it has a problem like me.

What happens when you debug my code?

Thank you for your help. I tried your code but it's not thing happened, Maybe I mistook.

Thank you IronRazer , your code are work.

### #9 IronRazer

• Custom Control Freak

Reputation: 1530
• Posts: 3,856
• Joined: 01-February 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 04 August 2015 - 05:33 PM

No problem, i just figured i would add that example for you or others that might want to use GraphicsPaths. They have a lot of nice features for drawing that can not be done or is difficult to do using standard drawing methods. 8)

### #10 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 05 August 2015 - 07:24 AM

I have one more question, if i will use "e.Graphics.TranslateTransform"
```   e.Graphics.TranslateTransform(Sx, Sy)
For Each gp As GraphicsPath In gpList
gpPen.Color = Color.Blue 'make sure the pen is set to the default color Blue
If gp Is SelectedGp Then gpPen.Color = Color.Gold 'if the current path is the selected path then set the pen color to Gold
e.Graphics.DrawPath(gpPen, gp)

Next
e.Graphics.Transform.Reset()
```

and Zoom function
```        For i As Integer = 0 To DataPoints.Length - 1
Dim gp As New GraphicsPath
Dim pnt2 As New Point(StartPoint.X + DataPoints(i).X*ZoomScale, StartPoint.Y + DataPoints(i).Y*ZoomScale)
StartPoint = pnt2
Next
```

because i tried to do that it's not work ,What should I do that ?

This post has been edited by little_prince41: 05 August 2015 - 08:51 AM

### #11 IronRazer

• Custom Control Freak

Reputation: 1530
• Posts: 3,856
• Joined: 01-February 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 05 August 2015 - 05:42 PM

Well, i skimmed through your other recent questions like this to see if i could find one that was using the Sx, Sy, and ZoomScale variables but, did not see these.

I would need to see the full code in order to tell how this application is functioning and how those 3 variables are being calculated in order to understand how it might be able to be done.

### #12 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 05 August 2015 - 09:59 PM

I'm sorry that I not showed all my code, this is my full code
```Imports System.Drawing.Drawing2D
Public Class Form1
Private gpList As New List(Of GraphicsPath)
Private SelectedGp As GraphicsPath
Private gpPen As New Pen(Color.Blue, 1)
Private StartPoint As New Point(100, 100)
Dim X() As Integer = {100, 100}
Dim Y() As Integer = {50, 200}
Private DataPoints() As Point
Dim largestX As Integer = Integer.MinValue 'X point Maximum
Dim smallestX As Integer = Integer.MaxValue 'X point Minimum
Dim largestY As Integer = Integer.MinValue 'Y point Maximum
Dim smallestY As Integer = Integer.MaxValue 'Y point Minimum
Private ZoomScale As Double
Dim ZoomEvent As Boolean = False
Dim Sx As Double 'Start X point
Dim Sy As Double 'Start Y point
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'Paint each Line/GraphicsPath in the List of GraphicsPaths
For Each gp As GraphicsPath In gpList
gpPen.Color = Color.Blue 'make sure the pen is set to the default color Blue
If gp Is SelectedGp Then gpPen.Color = Color.Gold 'if the current path is the selected path then set the pen color to Gold
e.Graphics.DrawPath(gpPen, gp)
Next
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
SelectedGp = Nothing
Using grx As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
For Each gp As GraphicsPath In gpList
If gp.IsOutlineVisible(e.Location, gpPen, grx) Then
SelectedGp = gp
Me.Text = "(" & gp.PathPoints(0).X.ToString & ", " & gp.PathPoints(0).Y.ToString & ")  (" & gp.PathPoints(1).X.ToString & ", " & gp.PathPoints(1).Y.ToString & ")"
Exit For
End If
Next
End Using
PictureBox1.Refresh()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
For i As Integer = gpList.Count - 1 To 0 Step -1
gpList(i).Dispose()
gpList.RemoveAt(i)
Next
'find Max and Min of X point
For Each elementX As Integer In X
largestX = Math.Max(largestX, elementX)
smallestX = Math.Min(smallestX, elementX)
Next
'find Max and Min of Y point
For Each elementY As Integer In Y
largestY = Math.Max(largestY, elementY)
smallestY = Math.Min(smallestY, elementY)
Next
'find ratio for fix picture size(all) to windows
If ZoomEvent = False Then
ZoomScale = (Me.PictureBox1.Height) / ((largestY - smallestY))
End If
Sx = (Me.PictureBox1.Width) + ((-smallestX - largestX) * ZoomScale) '
Sy = (Me.PictureBox1.Height) + ((-smallestY - largestY) * ZoomScale) '
StartPoint.X = (Sx / 2)
StartPoint.Y = (Sy / 2) '
'Add each line to a new GraphicsPath and add it to the List of GraphicsPaths
For i As Integer = 0 To DataPoints.Length - 1
Dim gp As New GraphicsPath
Dim pnt2 As New Point(StartPoint.X + (DataPoints(i).X * ZoomScale), StartPoint.Y + (DataPoints(i).Y * ZoomScale))
StartPoint = pnt2
Next
ZoomEvent = False
PictureBox1.Refresh()
End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
'Be sure to Dispose all the GraphicsPaths and the Pen when the form is closing
For i As Integer = gpList.Count - 1 To 0 Step -1
gpList(i).Dispose()
gpList.RemoveAt(i)
Next
gpPen.Dispose()
End Sub
' Zoom In function
Private Sub ZoomIn_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomIn_btn.Click
ZoomScale += 0.1
ZoomEvent = True
End Sub
' Zoom Out function
Private Sub ZoomOut_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomOut_btn.Click
ZoomScale -= 0.1
ZoomEvent = True
End Sub
```

### #13 IronRazer

• Custom Control Freak

Reputation: 1530
• Posts: 3,856
• Joined: 01-February 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 06 August 2015 - 03:47 PM

Well, you did not mention it had errors, you just said it did not work. When i tried it, it says there is an error on this line before it even starts.
```For i As Integer = 0 To DataPoints.Length - 1
```

That is because you have not added any points to the DataPoints. You need to add your data points to the DataPoints Array.

Once you get that working then, if you still need help, post your full code or the relevant code again with details on what you are having problems with. 8)

### #14 IronRazer

• Custom Control Freak

Reputation: 1530
• Posts: 3,856
• Joined: 01-February 13

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 07 August 2015 - 05:53 AM

Maybe this will help get you going in the right direction. It works but, i don`t know if it works the way you are aiming for. 8)
```Imports System.Drawing.Drawing2D

Public Class Form1
Private gpList As New List(Of GraphicsPath)
Private SelectedGp As GraphicsPath
Private gpPen As New Pen(Color.Blue, 1)
Private StartPoint As New Point(100, 100)
Private DataPoints() As Point = {New Point(100, 50), New Point(100, 200)}
Dim largestX As Integer
Dim smallestX As Integer
Dim largestY As Integer
Dim smallestY As Integer
Private ZoomScale As Double
Dim ZoomEvent As Boolean = False
Dim Sx As Double 'Start X point
Dim Sy As Double 'Start Y point

Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'Paint each Line/GraphicsPath in the List of GraphicsPaths
For Each gp As GraphicsPath In gpList
gpPen.Color = Color.Blue 'make sure the pen is set to the default color Blue
If gp Is SelectedGp Then gpPen.Color = Color.Gold 'if the current path is the selected path then set the pen color to Gold
e.Graphics.DrawPath(gpPen, gp)
Next
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
SelectedGp = Nothing
Using grx As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
For Each gp As GraphicsPath In gpList
If gp.IsOutlineVisible(e.Location, gpPen, grx) Then
SelectedGp = gp
Me.Text = "(" & gp.PathPoints(0).X.ToString & ", " & gp.PathPoints(0).Y.ToString & ")  (" & gp.PathPoints(1).X.ToString & ", " & gp.PathPoints(1).Y.ToString & ")"
Exit For
End If
Next
End Using
PictureBox1.Refresh()
End Sub

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

For i As Integer = gpList.Count - 1 To 0 Step -1
gpList(i).Dispose()
gpList.RemoveAt(i)
Next

'find Max and Min of X point
For Each element As Point In DataPoints
largestX = Math.Max(largestX, element.X)
smallestX = Math.Min(smallestX, element.X)
largestY = Math.Max(largestY, element.Y)
smallestY = Math.Min(smallestY, element.Y)
Next

'find ratio for fix picture size(all) to windows
If ZoomEvent = False Then
ZoomScale = Me.PictureBox1.Height / (largestY - smallestY)
End If
Sx = (Me.PictureBox1.Width) + ((-smallestX - largestX) * ZoomScale) '
Sy = (Me.PictureBox1.Height) + ((-smallestY - largestY) * ZoomScale) '
StartPoint.X = CInt((Sx / 2))
StartPoint.Y = CInt((Sy / 2)) '

'Add each line to a new GraphicsPath and add it to the List of GraphicsPaths
For i As Integer = 0 To DataPoints.Count - 1
Dim gp As New GraphicsPath
Dim pnt2 As New Point(CInt((StartPoint.X + (DataPoints(i).X) * ZoomScale)), CInt((StartPoint.Y + (DataPoints(i).Y) * ZoomScale)))
StartPoint = pnt2
Next
ZoomEvent = False
PictureBox1.Refresh()
End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
'Be sure to Dispose all the GraphicsPaths and the Pen when the form is closing
SelectedGp = Nothing
For i As Integer = gpList.Count - 1 To 0 Step -1
gpList(i).Dispose()
gpList.RemoveAt(i)
Next
gpPen.Dispose()
End Sub

' Zoom In function
Private Sub ZoomIn_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomIn_btn.Click
ZoomScale += 0.1
ZoomEvent = True
End Sub

' Zoom Out function
Private Sub ZoomOut_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomOut_btn.Click
ZoomScale -= 0.1
ZoomEvent = True
End Sub
End Class

```

### #15 little_prince41

Reputation: 3
• Posts: 163
• Joined: 10-August 07

## Re: How to know position line(X1,Y1,X2,Y2) when mouse click on Picturebox?

Posted 07 August 2015 - 08:27 AM

Thank you IronRazer, it's work