WPF C# 错误的鼠标坐标

WPF C# wrong mouse coordinates

所以我试图通过单击图像获取鼠标坐标,但它给出了错误的坐标。当我移动鼠标进行绘制时,线条出现在远离光标的位置。 这是我用来获取鼠标坐标的代码:

    private void ponaredek_MouseDown(object sender, MouseButtonEventArgs e)
    {
        mouseDown = true;
        //x1 = System.Windows.Forms.Control.MousePosition;
        x1 = new System.Drawing.Point((int)e.GetPosition(this).X, (int)e.GetPosition(this).Y);
    }

x1 是 System.Drawing.Point 类型(我需要绘图中的点,以便在 emgucv 中使用)。我该怎么做才能更正光标位置(我画了光标所在的位置)

您想获取鼠标相对于 Image 元素的位置,而不是 Window。所以替换

e.GetPosition(this)

来自

e.GetPosition((IInputElement)sender)

e.GetPosition(ponaredek)

如果这是图像元素。

它应该是这样的:

var pos = e.GetPosition((IInputElement)sender);
x1 = new System.Drawing.Point(pos.X, pos.Y);

还要确保图像元素的 Stretch 属性 设置为 None