I have a image editing program and need the X and the Y coordinate of the image. At the moment I use the MouseLeftButtonDown and MouseRightButtonDown event.
我有一个图像编辑程序,需要图像的X和Y坐标。现在我使用MouseLeftButtonDown和MouseRightButtonDown事件。
<Image Name="Image" MouseLeftButtonDown="MouseLeftButtonDown_Click" MouseRightButtonDown="MouseRightButtonDown_Click"></Image>
The problem I have is that I don't geht the right position on the picture in my WPF form. Means, if I drag the window smaller the coordinate is changing.
我遇到的问题是我没有在WPF表格中找到正确的位置。意思是,如果我把窗口拖小坐标就会改变。
My method looks like this:
我的方法是这样的:
private void MouseLeftButtonDown_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Windows.Point p = e.GetPosition(null);
MessageBox.Show(p.X.ToString());
}
I think that the problem is that I add null
as a argument in e.GetPosition
. But I don't know what I have to add here otherwise...
我认为问题是我在e。getposition中添加null作为参数。但是我不知道我还需要补充什么,否则……
For example I open a 1920x1080 image I really want 1920 and 1080 if I press the mouse at the right bottem corner.
例如,我打开了一个1920x1080的图像,我想要的是1920和1080,如果我把鼠标按在右边的底部。
3 个解决方案
#1
2
Sounds as if you want the position with respect to the pixels of the original image, no matter what size the containing window is.
听起来好像你想要的位置相对于原始图像的像素,无论包含窗口的大小。
This should help...
这应该有助于…
private void MouseLeftButtonDown_Click(object sender, MouseButtonEventArgs e)
{
System.Windows.Point p = e.GetPosition(image);
double pixelWidth = image.Source.Width;
double pixelHeight = image.Source.Height;
double x = pixelWidth * p.X / image.ActualWidth;
double y = pixelHeight * p.Y / image.ActualHeight;
MessageBox.Show(x + ", " + y);
}
I renamed your image to "image" to resolve the conflict between image name and class name. Update your XAML as follows:
我将您的映像重命名为“image”,以解决映像名和类名之间的冲突。更新您的XAML如下:
<Image Name="image" MouseLeftButtonDown="MouseLeftButtonDown_Click" MouseRightButtonDown="MouseRightButtonDown_Click"></Image>
pixelWidth and pixelHeight are the original width and height of the source image. x and y are calculated according the ratio between the original pixel width/height and the actual displayed width/height of the image on screen.
像素宽度和像素高度是源图像的原始宽度和高度。x和y是根据原始像素宽度/高度与屏幕上实际显示的图像宽度/高度之比来计算的。
To display whole pixels in the popup message, use this instead:
要在弹出消息中显示整个像素,请使用以下方法:
MessageBox.Show((int)x + ", " + (int)y);
@Gerret: please can you give a few examples of the wrong coordinates, and what you were expecting them to be?
@Gerret:你能举几个错误坐标的例子吗?你希望它们是什么?
#2
1
instead of null
try passing your Image
而不是空尝试传递您的图像。
Like System.Windows.Point p = e.GetPosition(Image);
像System.Windows。点p = e.GetPosition(图片);
For Example:
例如:
Your Image:
你的图片:
<Image x:Name="imgYouImage" />
In your code use:
在您的代码中使用:
Point p = e.GetPosition(imgYouImage);
#3
0
Try:
试一试:
System.Windows.Point p = e.GetPosition(Image);
As recommended by Simon Martin, I'll explain this...
根据西蒙·马丁的推荐,我将解释……
GetPosition() returns the position of the mouse click in relation to the position of the element passed in as an argument. If you pass in null, this will be relative to the position of the window, so you need to pass in your image name to get the correct coordinates.
GetPosition()返回鼠标单击与作为参数传入的元素的位置相关的位置。如果您传入null,这将相对于窗口的位置,因此您需要传入映像名以获得正确的坐标。
Please see my other answer for a better solution.
请参阅我的另一个答案,以获得更好的解决方案。
#1
2
Sounds as if you want the position with respect to the pixels of the original image, no matter what size the containing window is.
听起来好像你想要的位置相对于原始图像的像素,无论包含窗口的大小。
This should help...
这应该有助于…
private void MouseLeftButtonDown_Click(object sender, MouseButtonEventArgs e)
{
System.Windows.Point p = e.GetPosition(image);
double pixelWidth = image.Source.Width;
double pixelHeight = image.Source.Height;
double x = pixelWidth * p.X / image.ActualWidth;
double y = pixelHeight * p.Y / image.ActualHeight;
MessageBox.Show(x + ", " + y);
}
I renamed your image to "image" to resolve the conflict between image name and class name. Update your XAML as follows:
我将您的映像重命名为“image”,以解决映像名和类名之间的冲突。更新您的XAML如下:
<Image Name="image" MouseLeftButtonDown="MouseLeftButtonDown_Click" MouseRightButtonDown="MouseRightButtonDown_Click"></Image>
pixelWidth and pixelHeight are the original width and height of the source image. x and y are calculated according the ratio between the original pixel width/height and the actual displayed width/height of the image on screen.
像素宽度和像素高度是源图像的原始宽度和高度。x和y是根据原始像素宽度/高度与屏幕上实际显示的图像宽度/高度之比来计算的。
To display whole pixels in the popup message, use this instead:
要在弹出消息中显示整个像素,请使用以下方法:
MessageBox.Show((int)x + ", " + (int)y);
@Gerret: please can you give a few examples of the wrong coordinates, and what you were expecting them to be?
@Gerret:你能举几个错误坐标的例子吗?你希望它们是什么?
#2
1
instead of null
try passing your Image
而不是空尝试传递您的图像。
Like System.Windows.Point p = e.GetPosition(Image);
像System.Windows。点p = e.GetPosition(图片);
For Example:
例如:
Your Image:
你的图片:
<Image x:Name="imgYouImage" />
In your code use:
在您的代码中使用:
Point p = e.GetPosition(imgYouImage);
#3
0
Try:
试一试:
System.Windows.Point p = e.GetPosition(Image);
As recommended by Simon Martin, I'll explain this...
根据西蒙·马丁的推荐,我将解释……
GetPosition() returns the position of the mouse click in relation to the position of the element passed in as an argument. If you pass in null, this will be relative to the position of the window, so you need to pass in your image name to get the correct coordinates.
GetPosition()返回鼠标单击与作为参数传入的元素的位置相关的位置。如果您传入null,这将相对于窗口的位置,因此您需要传入映像名以获得正确的坐标。
Please see my other answer for a better solution.
请参阅我的另一个答案,以获得更好的解决方案。