wpf 如何获取显示图像的(x,y)坐标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13451388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:16:55  来源:igfitidea点击:

How to get (x, y) coordinates of displayed image?

c#wpfimageeventsuser-interface

提问by Hameer Abbasi

Currently, I'm using a WPF Imagecontrol to display images. I want to do the following: On a MouseUp, MouseDownor MouseMoveevent, I want to get the coordinates of the point under the cursor, not with reference to the top-left corner of the control, but with respect to the actual coordinates of the displayed bitmap, which may have been re-scaled, or may have a different aspect ratio than the control itself. If an alternate control provides this functionality, I'd be equally happy to hear about it. Thanks in advance.

目前,我使用 WPFImage控件来显示图像。我想执行以下操作: 在MouseUp,MouseDownMouseMoveevent 上,我想获取光标下点的坐标,不是参考控件的左上角,而是相对于显示位图的实际坐标,可能已重新缩放,或者可能具有与控件本身不同的纵横比。如果替代控件提供此功能,我也同样乐意听到。提前致谢。

EDIT: I'd also like so may to know it if the coordinates of the control were out of range of the image, due to a different aspect ratio.

编辑:由于不同的纵横比,如果控件的坐标超出图像范围,我也想知道。

回答by bitbonk

You can use the various TransformTo*methods of the Visualclass to get, coordnates relative to a sepcific control, it will take all transformation applied to the visual tree into account.

您可以使用该类的各种TransformTo*方法Visual来获取相对于特定控件的坐标,它将考虑应用于可视化树的所有转换。

Also, if you attach MouseUp, MouseDownand MouseMoveto the Imagecontrol itself, you should already get the correct coordinates from the MouseButtonEventArgsand if you use the mouse outside of the visual bounds of the control, you will not get those events, so you don't need extra code check for coordinates being out of bounds.

另外,如果你重视MouseUpMouseDown以及MouseMoveImage控制本身,你应该已经在得到正确的坐标MouseButtonEventArgs,如果您使用该控件的可视边界的鼠标之外,你不会得到这些事件,这样你就不会需要额外的代码检查坐标是否越界。

If your actualgoal is to find out which acutal pixel of your bitmap image has been touched by the mouse (as you would need for a bitmap/pixel editing software) things get much harder because WPF uses virtual device indendent pixels that do not directly relate to pixels on the screen or pixels in a bitmap that has been rendered in an Imagecontrol. The Imagecontrol internally scales a bitmap image based on the DPI settings of the bitmap file itself and based on the DPI settings of operating system.

如果您的实际目标是找出位图图像的哪个实际像素已被鼠标触摸(就像位图/像素编辑软件所需要的那样),事情会变得更加困难,因为 WPF 使用不直接相关的虚拟设备缩进像素到屏幕上的像素或已在Image控件中呈现的位图中的像素。该Image控件根据位图文件本身的 DPI 设置和操作系统的 DPI 设置在内部缩放位图图像。

回答by MarcD

you can get the coordinates of the mouse by this way :

您可以通过这种方式获取鼠标的坐标:

private void image1_MouseDown(object sender, MouseButtonEventArgs e)
{
Point point = e.GetPosition(image1); //position relative to the image
Point point2 = e.GetPosition(this);  //position relative to the window
}

Hope that answered a part of your question.

希望回答了您问题的一部分。