Image coordinates within a picturebox
-
How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks
-
How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks
Forget about it, I found an extended PictureBox: Mouse Position over Image in a PictureBox[^]
-
How can I know the coordinates of the image within the picturebox when clicked? Subject to: 1. The picturebox mode is Zoom 2. The picturebox can have any size (but the image inside is uniformely scaled because of Zoom mode) I would appreciate ideas for solving this, Thanks
-
Did you find an answer to this? If so, can you please share? I need to do exactly the same. Thanks.
Whats wrong with the one I posted?
-
Did you find an answer to this? If so, can you please share? I need to do exactly the same. Thanks.
The thing is that the PictureBoxExtended brings only one event (MouseMoveOverImage), and I needed to create another (MouseClickedImage), but its really easy: This is what it brings:
/// Handler for when the mouse moves over the image part of the picture box public delegate void MouseMoveOverImageHandler(object sender, MouseEventArgs e); /// Occurs when the mouse have moved over the image part of a picture box public event MouseMoveOverImageHandler MouseMoveOverImage;
And I added the following
/// Handler for when the mouse **clicks** over the image part of the picture box public delegate void MouseClickedImageHandler(object sender, MouseEventArgs e); /// Occurs when the mouse have **clicked** over the image part of a picture box public event MouseMoveOverImageHandler MouseClickedImage;
And then go down to the protected methods part and create a method that overrides the OnMouseClick method of the base class PictureBox, so:
protected override void OnMouseClick(MouseEventArgs e) { **base.OnMouseClick(e);** if (Image != null) { if (MouseClickedImage != null) { Point p = TranslatePointToImageCoordinates(e.Location); if (p.X >= 0 && p.X < Image.Width && p.Y >= 0 && p.Y < Image.Height) { MouseEventArgs ne = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta); MouseClickedImage(this, ne); } } } }
And what I wrote here is just like the code within the OnMouseMove method... so I didn't had to think anything... Look that i'm calling to the base method also, so you can still use the MouseClick event normally Cya!
-
The thing is that the PictureBoxExtended brings only one event (MouseMoveOverImage), and I needed to create another (MouseClickedImage), but its really easy: This is what it brings:
/// Handler for when the mouse moves over the image part of the picture box public delegate void MouseMoveOverImageHandler(object sender, MouseEventArgs e); /// Occurs when the mouse have moved over the image part of a picture box public event MouseMoveOverImageHandler MouseMoveOverImage;
And I added the following
/// Handler for when the mouse **clicks** over the image part of the picture box public delegate void MouseClickedImageHandler(object sender, MouseEventArgs e); /// Occurs when the mouse have **clicked** over the image part of a picture box public event MouseMoveOverImageHandler MouseClickedImage;
And then go down to the protected methods part and create a method that overrides the OnMouseClick method of the base class PictureBox, so:
protected override void OnMouseClick(MouseEventArgs e) { **base.OnMouseClick(e);** if (Image != null) { if (MouseClickedImage != null) { Point p = TranslatePointToImageCoordinates(e.Location); if (p.X >= 0 && p.X < Image.Width && p.Y >= 0 && p.Y < Image.Height) { MouseEventArgs ne = new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta); MouseClickedImage(this, ne); } } } }
And what I wrote here is just like the code within the OnMouseMove method... so I didn't had to think anything... Look that i'm calling to the base method also, so you can still use the MouseClick event normally Cya!