简体   繁体   English

如何在Picturebox中获取光标在图像上的确切位置

[英]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. _scale是缩放后的值,可能是10、50、100或200等。

My Question: 我的问题:

it give the correct value if the zoomed is greater than 100%. 如果缩放比例大于100%,则给出正确的值。 But is the zoomed is less than 100 it give the incorrect value. 但是如果缩放比例小于100,则会给出错误的值。 How can I give the correct value if even the zoomed is less than 100% ? 如果缩放比例小于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. 编辑:图片框的大小模式是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). (即0像素是由图片的起始位置而不是图片框指定的)。

Looks like the culprit is integer division: 10 / 100 = 0 罪魁祸首是整数除法: 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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