Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get mouse coordinates of image in picturebox

822 views
Skip to first unread message

Tom

unread,
Feb 25, 2005, 4:09:18 PM2/25/05
to
I have a picturebox on my VB.NET form. The picturebox size mode is set to
stretched. 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 stretched).
i.e. if the actual image has a width of 2000, and I move the cursor over to
the right side of the stretched 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?

Thanks.

Tom


Bob Powell [MVP]

unread,
Feb 26, 2005, 6:22:24 AM2/26/05
to
You need to take the current picturebox coordinate from PictureBox.MouseMove
and multiply it by the ratio of picturebox size to image size. Something
like...

point _imgPoint;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
_imgPoint=new Point(
(int)(((float)this.pictureBox1.Image.Width /
this.pictureBox1.Width)*e.X),
(int)(((float)this.pictureBox1.Image.Height /
this.pictureBox1.Height)*e.Y)
);
}

I didn't test it but you get the picture....

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Tom" <t...@nospam.com> wrote in message
news:ukQHF53G...@TK2MSFTNGP12.phx.gbl...

Tom

unread,
Mar 2, 2005, 10:05:50 AM3/2/05
to
Bob: Thanks, I tried out (in VB.NET):

Dim _imgPoint As Point = New Point(CInt((CSng(pbImage.Image.Width /
pbImage.Width) * e.X)), CInt((CSng(pbImage.Image.Height / pbImage.Height) *
e.Y)))

This works REALLY close in stretched mode... however, as the cursor moves
either to the extreme right (width) or extreme down (height) it seems to
miss pixels. For instance, in the debugger I moved the cursor over to the
farthest point right, which should have been 2000 (since the image is 2000
pixels wide); however, with the above calculation I could only get the X
value to 1990.... It's pretty close and I guess shouldn't be too much of an
issue (if they want an exact number they will have to select to show the
image in non-stretched form) - however, is there anything else I could do to
refine this calculation to give me more precise results?

Thanks.

Tom

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:O3UWhV$GFHA...@TK2MSFTNGP14.phx.gbl...

Bob Powell [MVP]

unread,
Mar 2, 2005, 11:05:57 AM3/2/05
to
Try using all doubles instead of singles. It would be best to convert all of
the sizes to doubles, do the math and then round the result to the nearest
integer.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Tom" <t...@nospam.com> wrote in message

news:ut8btlzH...@TK2MSFTNGP14.phx.gbl...

0 new messages