简体   繁体   中英

How can I get the exact location of cursor on image in Picturebox

I have the picture box in which the image may be zoomed with different values. Then I try to find out the location of cursor on picture box. I write the following code on picture box mouse move event:

int x = (2 * e.X - pictureBox1.Width + pictureBox1.Image.Width) / (2 * _scale / 100);
int y = (2 * e.Y - pictureBox1.Height + pictureBox1.Image.Height) / 2 * _scale / 100);

here _scale is is the zoomed value that may be 10,50,100,or 200 etc.

My Question:

it give the correct value if the zoomed is greater than 100%. But is the zoomed is less than 100 it give the incorrect value. How can I give the correct value if even the zoomed is less than 100% ?

Some More Explanation:

for zoomed I write the following code.

pictureBox1.Image =  new Bitmap(Orignal_image, (int)( Orignal_image .Width * scale / 100), (int)( Orignal_image.Height * scale / 100));

EDIT: The size mode of picturebox is centerImage. and it is not necessary that the picturebox width is equal to the image. its width may be less than image, and then the image show at the center of picturebox. i only need the location of image. (ie, the 0 pixel is given by start position of image not picturebox).

Looks like the culprit is integer division: 10 / 100 = 0

Use float division or rearrange the expression:

int x = (e.X - (pictureBox1.Width - pictureBox1.Image.Width) / 2) * 100 / _scale;
int x = e.X * 100 / _scale;
int y = e.Y * 100 / _scale;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM