473,747 Members | 2,596 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 22519
4,871 Recognized Expert Specialist
Here is a thread that may help:
Get Mouse coordinates of Image in PictureBox
May 13 '07 #2
692 Recognized Expert Contributor

Please refer this Link
May 13 '07 #3
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.
  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

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

Similar topics

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
by: Raju | last post by:
Is there any way to get and set the pixel value in a container control(form, picturebox...)?
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
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 then centered in the panel by using autoscrollposition. My problem is that I'm not sure of how to...
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.
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
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 not find anything that tells me about this in the internet. Does somebody here already did something...
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
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: My.Computer.FileSystem.DeleteFile(sPath)
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 most users, this new feature is actually very convenient. If you want to control the update process,...
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.