views:

113

answers:

2

Hi every one. 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. (i.e., the 0 pixel is given by start position of image not picturebox).

A: 

int x = e.X * 100 / _scale;
int y = e.Y * 100 / _scale;

danbystrom
no sir, its may be correct if the width of picturebox is equal to the width of image. but in my case it is not necessary.
qulzam
+2  A: 

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;
Kha
Yes sir, its correct but there is some problem that i want the result that left side start position give the 0 and to right it increase 1,2,3,....and so on. but it gives the left side -(maximum width) eg -1024 and it increases to the right side until it reach 0 at the right side.
qulzam
Whoops, I accidentally the signs. Try the corrected version.
Kha
Thanks Kha, its work fine.
qulzam