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]
3 22361
Hi,
Please refer this Link
Put this in MouseMove of the PictureBox: - //Declarations of few variables to minimize the number of refers to the Picture Box.
-
-
{
-
int imgWidth = PictureBox1.Image.Width;
-
int imgHeight = PictureBox1.Image.Height;
-
int boxWidth = PictureBox1.Size.Width;
-
int boxHeight = PictureBox1.Size.Height;
-
-
//This variable will hold the result
-
float X = e.X;
-
float Y = e.Y;
-
//Comparing the aspect ratio of both the control and the image itself.
-
if (imgWidth / imgHeight > boxWidth / boxHeight) {
-
//If true, that means that the image is stretched through the width of the control.
-
//'In other words: the image is limited by the width.
-
-
//The scale of the image in the Picture Box.
-
float scale = boxWidth / imgWidth;
-
-
//Since the image is in the middle, this code is used to determinate the empty space in the height
-
//'by getting the difference between the box height and the image actual displayed height and dividing it by 2.
-
float blankPart = (boxHeight - scale * imgHeight) / 2;
-
-
Y -= blankPart;
-
-
//Scaling the results.
-
X /= scale;
-
Y /= scale;
-
} else {
-
//If true, that means that the image is stretched through the height of the control.
-
//'In other words: the image is limited by the height.
-
-
//The scale of the image in the Picture Box.
-
float scale = boxHeight / imgHeight;
-
-
//Since the image is in the middle, this code is used to determinate the empty space in the width
-
//'by getting the difference between the box width and the image actual displayed width and dividing it by 2.
-
float blankPart = (boxWidth - scale * imgWidth) / 2;
-
X -= blankPart;
-
-
//Scaling the results.
-
X /= scale;
-
Y /= scale;
-
}
-
Label1.Text = Conversion.Int(X) + ", " + Conversion.Int(Y);
-
}
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...
|
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...
|
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:
...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |