By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Getting the x:y coordinates of the PictureBox Image

P: 1
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
Share this Question
Share on Google+
3 Replies


kenobewan
Expert 2.5K+
P: 4,871
Here is a thread that may help:
Get Mouse coordinates of Image in PictureBox
May 13 '07 #2

shweta123
Expert 100+
P: 692
Hi,

Please refer this Link
May 13 '07 #3

P: 1
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.
  2.  
  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;
  8.  
  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.
  16.  
  17.         //The scale of the image in the Picture Box.
  18.         float scale = boxWidth / imgWidth;
  19.  
  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;
  23.  
  24.         Y -= blankPart;
  25.  
  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.
  32.  
  33.         //The scale of the image in the Picture Box.
  34.         float scale = boxHeight / imgHeight;
  35.  
  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;
  40.  
  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.