473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the x:y coordinates of the PictureBox Image

1 New Member
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.imag e.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 22361
kenobewan
4,871 Recognized Expert Specialist
Here is a thread that may help:
Get Mouse coordinates of Image in PictureBox
May 13 '07 #2
shweta123
692 Recognized Expert Contributor
Hi,

Please refer this Link
May 13 '07 #3
Megadardery
1 New Member
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
2091
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 know the x,y coords (in pixels) of where he is pointing to. Here is what I have done
1
3694
by: Raju | last post by:
Is there any way to get and set the pixel value in a container control(form, picturebox...)?
1
11901
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 click in relation
2
1484
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 then the panel, I can scroll to a different part of it. What I'm looking to do is when a user clicks on the image,the point at which he/she clicks is...
0
3866
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 your help, Here is the code.
4
8825
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. Thanks, Henry
1
3365
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 fill two text boxes with the X and Y coordinates of that click, but the coordinates need to be based in the image only not in the whole form. I did...
5
10298
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 Picture1.Width Or y Picture1.Height x = x / 1.01
4
5554
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 want to move or delete the file that is being shown. The corresponding event- driven code for "delete this picture" uses: ...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.