我有一个在 WPF 中的 Image 对象内显示图像的应用程序。该图像包含在一个控件中,其 xaml 如下所示:
<UserControl x:Class="MyProgram.NativeImageDisplay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Loaded="UserControl_Loaded">
<Canvas Name="border" Background="Black" >
<Image Name="image" StretchDirection="Both" Stretch="Uniform" ClipToBounds="True"
SnapsToDevicePixels="True" RenderOptions.BitmapScalingMode="HighQuality"></Image>
</Canvas>
</UserControl>
其中两个控件包含在窗口的网格中,如下所示:
<Grid Grid.Row="2" Name="DisplayCanvas">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<l:NativeImageDisplay x:Name="imageDisplay2" Grid.Column="1" ClipToBounds="True"/>
<l:NativeImageDisplay x:Name="imageDisplay" Grid.Column="0" ClipToBounds="True"/>
</Grid>
我一直在呼吁剪辑是真实的。
用户可以使用鼠标上的滚动按钮放大图像,最终调用图像上的 ScaleTransform:
private void image_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (!thumbnail)
{
TransformGroup transformGroup = (TransformGroup)border.RenderTransform;
ScaleTransform transform = (ScaleTransform)transformGroup.Children[0];
double oldScaleX = transform.ScaleX;
double oldScaleY = transform.ScaleY;
double zoom = e.Delta;
transform.ScaleX += zoom / 10000;
transform.ScaleY += zoom / 10000;
if (transform.ScaleX > maxZoom || transform.ScaleY > maxZoom)
{
transform.ScaleX = maxZoom;
transform.ScaleY = maxZoom;
}
if (transform.ScaleX < minZoom || transform.ScaleY < minZoom)
{
transform.ScaleX = minZoom;
transform.ScaleY = minZoom;
}
Point thePoint = e.GetPosition(border);
transform.CenterY = 0;
transform.CenterX = 0;
foreach (UIElement child in border.Children)
{
if (child is Anchor)
{
TransformGroup group = (TransformGroup)child.RenderTransform;
ScaleTransform t = (ScaleTransform)group.Children[0];
t.ScaleX *= oldScaleX / transform.ScaleX;
t.ScaleY *= oldScaleY / transform.ScaleY;
}
}
}
}
一旦调用了缩放变换,图像就不再包含在其画布的边界或网格选择中。本质上, ClipToBounds 被忽略了。我怎样才能让这个变换注意 ClipToBounds?