7

我正在尝试学习 WPF,所以这是一个简单的问题,我希望:

我有一个包含 Image 元素的窗口,该元素绑定到具有用户可配置Stretch属性的单独数据对象

<Image Name="imageCtrl" Source="{Binding MyImage}" Stretch="{Binding ImageStretch}" />

当用户将鼠标移到图像上时,我想确定鼠标相对于原始图像的坐标(在控件中显示时发生拉伸/裁剪之前),然后对这些坐标进行操作(更新图像)。

我知道我可以通过 Image 控件向 MouseMove 事件添加事件处理程序,但我不确定如何最好地转换坐标:

void imageCtrl_MouseMove(object sender, MouseEventArgs e)
{
    Point locationInControl = e.GetPosition(imageCtrl);
    Point locationInImage = ???
    updateImage(locationInImage);
}

现在我知道我可以比较控件的大小,然后打开Source以计算 X 和 Y 上的标量和偏移量,然后自己进行转换。但是 WPF 已经拥有所有信息,这似乎是 WPF 库中内置的功能。所以我想知道:是否有一个简短而甜蜜的解决方案?还是我需要自己写这个?ActualSizeimageCtrl.Stretch


编辑我正在附加我当前的,不是那么短而甜蜜的解决方案。它还不错,但是如果 WPF 没有自动提供此功能,我会感到有些惊讶:

Point ImgControlCoordsToPixelCoords(Point locInCtrl, 
    double imgCtrlActualWidth, double imgCtrlActualHeight)
{
    if (ImageStretch == Stretch.None)
        return locInCtrl;

    Size renderSize = new Size(imgCtrlActualWidth, imgCtrlActualHeight);
    Size sourceSize = bitmap.Size;

    double xZoom = renderSize.Width / sourceSize.Width;
    double yZoom = renderSize.Height / sourceSize.Height;

    if (ImageStretch == Stretch.Fill)
        return new Point(locInCtrl.X / xZoom, locInCtrl.Y / yZoom);

    double zoom;
    if (ImageStretch == Stretch.Uniform)
        zoom = Math.Min(xZoom, yZoom);
    else // (imageCtrl.Stretch == Stretch.UniformToFill)
        zoom = Math.Max(xZoom, yZoom);

    return new Point(locInCtrl.X / zoom, locInCtrl.Y / zoom);
}
4

1 回答 1

8

如果您使用 ViewBox,可能会更容易。例如:

<Viewbox Stretch="{Binding ImageStretch}">
    <Image Name="imageCtrl" Source="{Binding MyImage}" Stretch="None"/>
</Viewbox>

然后当你去调用 GetPosition(..) WPF 将自动考虑缩放。

void imageCtrl_MouseMove(object sender, MouseEventArgs e) 
{ 
    Point locationInControl = e.GetPosition(imageCtrl);
}
于 2010-04-04T06:09:30.650 回答