Read picture box mouse coordinates on click
Asked Answered
A

4

15

I have a Picture Box with a picture loaded and I want to read the location (as in x,y inside the Picture Box) when I click the image; is this possible ? Even more, can i read these coordinates (Points) when i mouse over ?

I know i have to use the events given (Mouse Click and Mouse Over) but don't know how to read the coordinates where the mouse pointer happens to be.

Avaricious answered 4/8, 2013 at 7:56 Comment(0)
T
35

Though other answers are correct let me add my point to it. You've pointed that you need to hook up MouseClick or MouseOver events for this purpose. Actually that is no need to hook those events to get Coordinates, you can get the Coordinates in just Click event itself.

private void pictureBox1_Click(object sender, EventArgs e)
{
    MouseEventArgs me = (MouseEventArgs)e;
    Point coordinates = me.Location;
}

The above code works since Click event's e argument wraps MouseEventArgs you can just cast it and make use of it.

Tahsildar answered 4/8, 2013 at 8:35 Comment(3)
This is the answer if OP wants to (I guess obviously) get the click point coordinates relative to the picturebox.Tillage
This is correct as long as there are no transformations on the image as it is rendered in the PictureBox. If the rendered version is stretched, scaled, or panned, then the MouseEventArgs.Location property will need identical transformations applied to get the click in image coordinates.Embark
@Embark what does transformations has to do with getting the clicked location of picture box? Op needs mouse coordinates with respect to picture box not image inside it.Tahsildar
P
5

You can get the X and Y coordinates as follows,

 this.Cursor = new Cursor(Cursor.Current.Handle);

  int xCoordinate = Cursor.Position.X;
  int yCoordinate = Cursor.Position.Y;

If you want to get the coordinate within the picture box, use the following code,

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    int xCoordinate = e.X;
    int yCoordinate = e.Y;
}
Pedestrianism answered 4/8, 2013 at 8:3 Comment(4)
you don't need all of that for mouseUp but you can use it in mouse_hoverCalendar
This gives the coordinates relative to the FORM. Not to the PictureBox boundary...Tillage
Isn't this answer pretty much identical as @NoIdeaForName's answer?Mcgean
@StigCoder04 Yes, they are identical except the timestamp they are posted on :)Pedestrianism
C
5

i'll just sum up the answers:

in MouseClick, MouseUp and a lot of other events you have the MouseEventArgs which contains Location of the mouse.

in MouseHover however you don't have MouseEventArgs and therefor, if you need the cursor's location, use Coder example:

  private void Form1_MouseHover(object sender, EventArgs e)
  {
     this.Cursor = new Cursor(Cursor.Current.Handle);

     int xCoordinate = Cursor.Position.X;
     int yCoordinate = Cursor.Position.Y;
  }
Calendar answered 4/8, 2013 at 8:14 Comment(0)
G
1

What about hooking up the MouseUp event and then getting the location from the MouseEventArgs?

Like this:

private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
    Point mousePointerLocation = e.Location;
}
Gaily answered 4/8, 2013 at 7:58 Comment(2)
It works with just a change : Point MouseLoc = e.Location; Thanks!Avaricious
Yes I figured out my mistake after writing it as well. Thanks for pointing out! Glad it helped! :)Gaily

© 2022 - 2024 — McMap. All rights reserved.