我如何计算正确的坐标?

问题描述 投票:-2回答:1

我需要帮助。我有一个Picturebox,现在想计算图片上的给定坐标,然后在标签中播放它们。我怎样才能做到最好?

如图所示。如果然后单击图像,则数据将输入到列表框中。

谢谢您的帮助。

我的图片在这里:https://prnt.sc/puxyu6

c# wpf wpf-controls coordinates
1个回答
-1
投票

在WPF中,这一次可能是最难的。 WPF / UWP是为MVVM设计的,除了在MVVM之外编程的初学者,我不认识任何人。而且我想不出用MVVM做到这一点的方法。

PictureBox也是WinForms元素。 The WPF equivalent称为Image

这样的导航辅助工具不是一件小事。原因之一是很少。但这归结为几步过程:

  1. 获取被单击的x和y像素坐标,也与Image的整体显示大小有关。通常,MouseClick Event将是该工具,但我找不到它。 MouseDown或LeftMouseDown是最近的事件。

  2. 如果整个图像都没有缩放或裁剪显示,那么现在只是简单的数学运算。如果它是X轴的20%和Y轴的21%,则很容易找出SourceImage上X的20%和Y的21%的位置。

  3. 如果有任何缩放或裁切,则必须考虑,否则为2。

  4. 将图像像素位置与您知道的坐标相等。

第1部分将看起来像这样,需要在图像的MouseDown或LeftMouseDown事件中注册:

private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
  //Unpack the sender
  Image source = (Image)Sender;

  //Get the click point
  Point clickPoint = e.GetPosition(source);

  //There is more then 1 height Property in WPF. 
  //The Actuall one is what you are looking for
  //Unfortunately you get doubles for this and int for the other. Consider general advise regarding double math
  double ElementHeight = source.ActualHeight;
  double ElementWidth = source.ActualWidth;

  //Do what you need to find the relative position

  //Part 2
}

希望其他人可以给您更好的答案。

© www.soinside.com 2019 - 2024. All rights reserved.