Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a picturebox,I could get the mouse's (x,y) from mouse double click event ,but how to conver the (x,y) to bitmap's real (x,y)?
Posted
Comments
Sergey Alexandrovich Kryukov 8-Aug-13 0:16am    
Easy, but don't use PictureBox. Why?
—SA
_Maxxx_ 8-Aug-13 1:10am    
Perhaps the OP has good reason, perhaps not.
Surely it would be more helpful to let them know the easy answer rather than telling them not to use a PictureBox?
Sergey Alexandrovich Kryukov 8-Aug-13 1:14am    
Who knows. Anyway, PictureBox is totally redundant control, so, as soon as anything just a bit mode complex than a static image is needed, using PictureBox becomes impractical, to say the least.
More helpful? Yes, I can help, my comment was just a placeholder. You answer, by the way, is not quite adequate...
—SA

C#
private void pictureBox1_Click(object sender, EventArgs e)
{
  Point topLeft = PointToScreen(new Point(pictureBox1.Left, pictureBox1.Top));
  int x = MousePosition.X - topLeft.X;
  int y = MousePosition.Y - topLeft.Y;

  MessageBox.Show(string.Format("X{0} Y{1}", x, y));
}


So use PointToScreen to find the coordinates of the picturebox within the screen.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Aug-13 1:22am    
If this is the same image shown in the picture box, these calculations are totally redundant: the bitmap coordinates are exactly the same as control's. Just think about it.
—SA
_Maxxx_ 8-Aug-13 6:00am    
I don't see what you mean. The mouse coordinates are relative to the screen while the OP wanted to convert that to the coordinates on the bitmap.
Assuming the image takes up the whole picture box then my calculation is right, isn't it?

I'm sure there are 57 other ways of doing it - but I think my code caters exactly for what the OP requested.
Here is the problem: you mention "bitmap coordinates", and don't mention what to click on. If we assume that this is a click on a PictureBox, the pixel coordinates are exactly the same as the coordinates of this control. That's all.

(If your control is different (but why?), you can re-calculate the coordinate translation throw screen coordinates or taking into account control's locations.)

Now, using PictureBox is questionable. It may or may not be useful, but usually not: it creates hassles without any help, only eats additional resources and even more of your development time to be spent for nothing. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900