下面就是关键代码:
XAML代码:
<Grid.Resources>
<TransformGroup x:Key="ImageCompareResources">
<ScaleTransform />
<TranslateTransform/>
</TransformGroup>
</Grid.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Disabled" Grid.Row="0" Grid.Column="0" x:Name="MasterScrollViewer" Margin="5" Background="WhiteSmoke">
<ContentControl x:Name="TestContentControl1"
MouseLeftButtonDown="MasterImage_MouseLeftButtonDown"
MouseLeftButtonUp="MasterImage_MouseLeftButtonUp"
MouseMove="MasterImage_MouseMove"
MouseWheel="MasterImage_MouseWheel">
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
x:Name="MasterImage" Source="{Binding Path=MasterImagePath}" Stretch="Uniform"
RenderTransform="{StaticResource ImageCompareResources}"/>
</ContentControl>
</ScrollViewer>
<TransformGroup x:Key="ImageCompareResources">
<ScaleTransform />
<TranslateTransform/>
</TransformGroup>
</Grid.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Disabled" Grid.Row="0" Grid.Column="0" x:Name="MasterScrollViewer" Margin="5" Background="WhiteSmoke">
<ContentControl x:Name="TestContentControl1"
MouseLeftButtonDown="MasterImage_MouseLeftButtonDown"
MouseLeftButtonUp="MasterImage_MouseLeftButtonUp"
MouseMove="MasterImage_MouseMove"
MouseWheel="MasterImage_MouseWheel">
<Image RenderOptions.BitmapScalingMode="NearestNeighbor"
x:Name="MasterImage" Source="{Binding Path=MasterImagePath}" Stretch="Uniform"
RenderTransform="{StaticResource ImageCompareResources}"/>
</ContentControl>
</ScrollViewer>
C#代码:
private void MasterImage_MouseWheel(object sender, MouseWheelEventArgs e)
{
ContentControl image = sender as ContentControl;
if (image == null)
{
return;
}
TransformGroup group = ImageComparePanel.FindResource("ImageCompareResources") as TransformGroup;
Debug.Assert(group != null, "Can't find transform group from image compare panel resource");
Point point = e.GetPosition(image);
double scale = e.Delta * 0.001;
ZoomImage(group, point, scale);
}
private static void ZoomImage(TransformGroup group, Point point, double scale)
{
Debug.Assert(group != null, "Oops, ImageCompareResources is removed from current control's resouce");
Point pointToContent = group.Inverse.Transform(point);
ScaleTransform transform = group.Children[0] as ScaleTransform;
if (transform.ScaleX + scale < 1)
{
return;
}
transform.ScaleX += scale;
transform.ScaleY += scale;
TranslateTransform transform1 = group.Children[1] as TranslateTransform;
transform1.X = -1 * ((pointToContent.X * transform.ScaleX) - point.X);
transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y);
}
private void MasterImage_MouseMove(object sender, MouseEventArgs e)
{
ContentControl image = sender as ContentControl;
if (image == null)
{
return;
}
if (this.isMouseLeftButtonDown && e.LeftButton == MouseButtonState.Pressed)
{
this.DoImageMove(image, e.GetPosition(image));
}
}
private void DoImageMove(ContentControl image, Point position)
{
TransformGroup group = ImageComparePanel.FindResource("ImageCompareResources") as TransformGroup;
Debug.Assert(group != null, "Can't find transform group from image compare panel resource");
TranslateTransform transform = group.Children[1] as TranslateTransform;
transform.X += position.X - this.previousMousePoint.X;
transform.Y += position.Y - this.previousMousePoint.Y;
this.previousMousePoint = position;
}
{
ContentControl image = sender as ContentControl;
if (image == null)
{
return;
}
TransformGroup group = ImageComparePanel.FindResource("ImageCompareResources") as TransformGroup;
Debug.Assert(group != null, "Can't find transform group from image compare panel resource");
Point point = e.GetPosition(image);
double scale = e.Delta * 0.001;
ZoomImage(group, point, scale);
}
private static void ZoomImage(TransformGroup group, Point point, double scale)
{
Debug.Assert(group != null, "Oops, ImageCompareResources is removed from current control's resouce");
Point pointToContent = group.Inverse.Transform(point);
ScaleTransform transform = group.Children[0] as ScaleTransform;
if (transform.ScaleX + scale < 1)
{
return;
}
transform.ScaleX += scale;
transform.ScaleY += scale;
TranslateTransform transform1 = group.Children[1] as TranslateTransform;
transform1.X = -1 * ((pointToContent.X * transform.ScaleX) - point.X);
transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y);
}
private void MasterImage_MouseMove(object sender, MouseEventArgs e)
{
ContentControl image = sender as ContentControl;
if (image == null)
{
return;
}
if (this.isMouseLeftButtonDown && e.LeftButton == MouseButtonState.Pressed)
{
this.DoImageMove(image, e.GetPosition(image));
}
}
private void DoImageMove(ContentControl image, Point position)
{
TransformGroup group = ImageComparePanel.FindResource("ImageCompareResources") as TransformGroup;
Debug.Assert(group != null, "Can't find transform group from image compare panel resource");
TranslateTransform transform = group.Children[1] as TranslateTransform;
transform.X += position.X - this.previousMousePoint.X;
transform.Y += position.Y - this.previousMousePoint.Y;
this.previousMousePoint = position;
}