By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,638 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,638 IT Pros & Developers. It's quick & easy.

Get Mouse coordinates of Image in PictureBox

P: n/a
Tom
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You are on the right track using the Picture box MouseMove event, here is a sample:
Put this above your class:
Private LocalMousePosition As Point

and this in your Picturebox MouseMove event:

LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

TextBox2.Text = ("X=" & LocalMousePosition.X & "," & "Y= " & LocalMousePosition.Y)

This will give you the correct position within your Image. I have a better example of this at this link:

http://www.gotdotnet.com/Community/U...0-e3d1ca40c74a

james
aka: Trucker


"Tom" <to*@nospam.com> wrote in message news:u0**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2

P: n/a
Tom
James: Hmm, it doesn't seem to be working. Using the code you gave me gives
me the same results as if I used:

TextBox2.Text = ("X=" & e.X & "," & "Y= " & e.Y)

in the PictureBox MouseMove event.

Remember, the image in the picture box is stretched to fill the picture box;
the actual image underneath is much bigger. So, say, pointing the mouse at
location 500x500 in the picturebox (i.e. the e.X and e.Y position) image
might actually be the pixal at 900x900 in the actual underlying image.

I'll take a look at your code in your referenced URL, but got any more
ideas?

Tom

"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl...
You are on the right track using the Picture box MouseMove event, here is a sample: Put this above your class:
Private LocalMousePosition As Point

and this in your Picturebox MouseMove event:

LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

TextBox2.Text = ("X=" & LocalMousePosition.X & "," & "Y= " & LocalMousePosition.Y)
This will give you the correct position within your Image. I have a better example of this at this link:
http://www.gotdotnet.com/Community/U...0-e3d1ca40c74a
james
aka: Trucker


"Tom" <to*@nospam.com> wrote in message

news:u0**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #3

P: n/a
Ack!! I forgot that the code I gave you (and the sample I linked to) both depend on a Picturebox set to AutoSize and the
Picturebox being on top of a Panel Control.........<blush>(to allow scrolling of the image)
I believe that you can still accomplish what you want ,but, it will require a few more things to get it working.
In the sample I linked to, you can resize the image up and down , and still get the proper X & Y coordinates. And that is also
what the code snippet I gave depends on.
I think you will need to not use the PictureBox for your image.
You might want to checkout Bob Powell's GDI+ FAQ at this link: http://www.bobpowell.net/gdiplus_faq.htm

Also, you might post your question in this newsgroup:
microsoft.public.dotnet.framework.drawing
There are a lot of people there very familiar with what you are trying to do.
Good luck.
james

"Tom" <to*@nospam.com> wrote in message news:eS*************@tk2msftngp13.phx.gbl...
James: Hmm, it doesn't seem to be working. Using the code you gave me gives
me the same results as if I used:

TextBox2.Text = ("X=" & e.X & "," & "Y= " & e.Y)

in the PictureBox MouseMove event.

Remember, the image in the picture box is stretched to fill the picture box;
the actual image underneath is much bigger. So, say, pointing the mouse at
location 500x500 in the picturebox (i.e. the e.X and e.Y position) image
might actually be the pixal at 900x900 in the actual underlying image.

I'll take a look at your code in your referenced URL, but got any more
ideas?

Tom

"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl...
You are on the right track using the Picture box MouseMove event, here is

a sample:
Put this above your class:
Private LocalMousePosition As Point

and this in your Picturebox MouseMove event:

LocalMousePosition = PictureBox1.PointToClient(Cursor.Position)

TextBox2.Text = ("X=" & LocalMousePosition.X & "," & "Y= " &

LocalMousePosition.Y)

This will give you the correct position within your Image. I have a

better example of this at this link:

http://www.gotdotnet.com/Community/U...0-e3d1ca40c74a

james
aka: Trucker


"Tom" <to*@nospam.com> wrote in message

news:u0**************@TK2MSFTNGP12.phx.gbl...
>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
>
>



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.