473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 22228
kenobewan
4,871 Expert 4TB
Here is a thread that may help:
Get Mouse coordinates of Image in PictureBox
May 13 '07 #2
shweta123
692 Expert 512MB
Hi,

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.
  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

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

Similar topics

6
by: CoreyMas | last post by:
Hello All, Here is what I would like to do I am creating a game program that displays a map (preferably in hexes but that is another matter) and whenever the user is over the "map" I want to...
1
by: Raju | last post by:
Is there any way to get and set the pixel value in a container control(form, picturebox...)?
1
by: Typpo | last post by:
Hi all, I'm not sure where to start. Using a PictureBox's click event: private void pb_Click(object sender, EventArgs e) { // ??? } ....how would I retrieve the coordinates of the mouse...
2
by: Rob | last post by:
Hi, I have a form which has a panel which has a picturebox inside of that. When a user clicks on the picture, it zooms by x2. The panel has autoscroll set to true, so when the image is larger...
0
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for...
4
by: Henry Wu | last post by:
Hi, I see examples of Magnifying an area under mouse coordinates to a form or picturebox using VB6 and VC6, does anyone know how to do it under VB.NET? Its nice to use the new GDI+ for it. ...
1
by: emferrari | last post by:
Hi all I'm developing a windows form application that has a picturebox in which I'll load screen prints from a jpg file. I want the user to click in one point in the image and after the click...
5
by: GoGs | last post by:
How this vb6 code convert in c# dotnet2 ----- Image1.Visible = False Image1.Picture = LoadPicture("c:\image008.jpg") Dim x, y As Single x = Image1.Width y = Image1.Height Do While x...
4
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.