如何计算正确的坐标?

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

如图所示。 如果您随后单击图像,则会在列表框中输入数据。

谢谢您的帮助。

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

pipippip 回答:如何计算正确的坐标?

在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%,则很容易弄清楚X的20%和Y的21%在SourceImage上的位置。

  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
}

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

本文链接:https://www.f2er.com/3129196.html

大家都在问