如何将鼠标坐标转换为位图的坐标? [英] how to convert mouse coordinate to bitmap's coordinate?
本文介绍了如何将鼠标坐标转换为位图的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个图片框,我可以从鼠标双击事件中获取鼠标(x,y),但如何将(x,y)转换为位图的真实(x,y)?
解决方案
private void pictureBox1_Click( object sender,EventArgs e)
{
Point topLeft = PointToScreen( new Point (pictureBox1.Left,pictureBox1.Top));
int x = MousePosition.X - topLeft.X;
int y = MousePosition.Y - topLeft.Y;
MessageBox.Show( string .Format( X {0} Y {1},x,y));
}
因此,请使用PointToScreen在屏幕内查找图片框的坐标。
问题在于:你提到位图坐标,并没有提到要点击的内容。如果我们假设这是点击PictureBox
,则像素坐标与此控件的坐标完全相同。就这样。
(如果您的控件不同(但为什么?),您可以重新计算坐标平移投影屏幕坐标或考虑控件的位置。)
现在,使用PictureBox
是值得怀疑的。它可能有用,也可能没有用,但通常不会:它会在没有任何帮助的情况下造成麻烦,只会占用额外的资源,甚至会花费更多的开发时间。请查看我过去的答案:
在其中附加图片图片框 [ ^ ],
画一个矩形C# [ ^ ],
如何从旧图纸中清除面板 [ ^ ]。
-SA
I have a picturebox,I could get the mouse's (x,y) from mouse double click event ,but how to conver the (x,y) to bitmap's real (x,y)?
解决方案
private void pictureBox1_Click(object sender, EventArgs e) { Point topLeft = PointToScreen(new Point(pictureBox1.Left, pictureBox1.Top)); int x = MousePosition.X - topLeft.X; int y = MousePosition.Y - topLeft.Y; MessageBox.Show(string.Format("X{0} Y{1}", x, y)); }
So use PointToScreen to find the coordinates of the picturebox within the screen.
Here is the problem: you mention "bitmap coordinates", and don't mention what to click on. If we assume that this is a click on aPictureBox
, the pixel coordinates are exactly the same as the coordinates of this control. That's all.
(If your control is different (but why?), you can re-calculate the coordinate translation throw screen coordinates or taking into account control's locations.)
Now, usingPictureBox
is questionable. It may or may not be useful, but usually not: it creates hassles without any help, only eats additional resources and even more of your development time to be spent for nothing. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].
—SA
这篇关于如何将鼠标坐标转换为位图的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文