469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Getting the x:y coordinates of the PictureBox Image

I have a picturebox on my C# .NET form. The picturebox size mode is set to
zoom. I then load an image into that form and display it. As the user
moves the mouse over the form, I want to get and display (in the status bar)
the image coordinates of the mouse location. However, if I use the
picturebox's MouseMove event, I am getting the coordinates of the mouse over
the PICTUREBOX, not the actual image underneath that (which is zoomed).
i.e. if the actual image has a width of 2000, and I move the cursor over to
the right side of the zoomed picturebox image, I am only getting a left
pixel value of around 1000 - because it is getting the mouse x:y coordinates
of the picture box, not the actual image.

How can I accomplish this? There isn't any picturebox.image.mousemove event
that I can see. How can I translate what the picturebox gives me back to the
coordinates of the actual image underneath it? Or have I got to go about
this differently?

Andrzej [email removed]
May 12 '07 #1
3 19266
4,871 Expert 4TB
Here is a thread that may help:
Get Mouse coordinates of Image in PictureBox
May 13 '07 #2
692 Expert 512MB

Please refer this Link
May 13 '07 #3
Put this in MouseMove of the PictureBox:

Expand|Select|Wrap|Line Numbers
  1. //Declarations of few variables to minimize the number of refers to the Picture Box.
  3. {
  4.     int imgWidth = PictureBox1.Image.Width;
  5.     int imgHeight = PictureBox1.Image.Height;
  6.     int boxWidth = PictureBox1.Size.Width;
  7.     int boxHeight = PictureBox1.Size.Height;
  9.     //This variable will hold the result
  10.     float X = e.X;
  11.     float Y = e.Y;
  12.     //Comparing the aspect ratio of both the control and the image itself.
  13.     if (imgWidth / imgHeight > boxWidth / boxHeight) {
  14.         //If true, that means that the image is stretched through the width of the control.
  15.         //'In other words: the image is limited by the width.
  17.         //The scale of the image in the Picture Box.
  18.         float scale = boxWidth / imgWidth;
  20.         //Since the image is in the middle, this code is used to determinate the empty space in the height
  21.         //'by getting the difference between the box height and the image actual displayed height and dividing it by 2.
  22.         float blankPart = (boxHeight - scale * imgHeight) / 2;
  24.         Y -= blankPart;
  26.         //Scaling the results.
  27.         X /= scale;
  28.         Y /= scale;
  29.     } else {
  30.         //If true, that means that the image is stretched through the height of the control.
  31.         //'In other words: the image is limited by the height.
  33.         //The scale of the image in the Picture Box.
  34.         float scale = boxHeight / imgHeight;
  36.         //Since the image is in the middle, this code is used to determinate the empty space in the width
  37.         //'by getting the difference between the box width and the image actual displayed width and dividing it by 2.
  38.         float blankPart = (boxWidth - scale * imgWidth) / 2;
  39.         X -= blankPart;
  41.         //Scaling the results.
  42.         X /= scale;
  43.         Y /= scale;
  44.     }
  45.     Label1.Text = Conversion.Int(X) + ", " + Conversion.Int(Y);
  46. }
Oct 15 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Raju | last post: by
1 post views Thread by Typpo | last post: by
reply views Thread by akh | last post: by
4 posts views Thread by Henry Wu | last post: by
1 post views Thread by emferrari | last post: by
5 posts views Thread by GoGs | last post: by
4 posts views Thread by gerardianlewis | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.