<Window x:Class="WpfApp120.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp120" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid x:Name="MainPanel"> <Grid.Resources> <TransformGroup x:Key="ImageTransformResource"> <ScaleTransform /> <TranslateTransform /> </TransformGroup> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Rectangle Grid.Column="0" x:Name="MasterImage" MouseLeftButtonDown="MasterImage_MouseLeftButtonDown" MouseLeftButtonUp="MasterImage_MouseLeftButtonUp" MouseMove="MasterImage_MouseMove" MouseWheel="MasterImage_MouseWheel"> <Rectangle.Fill> <VisualBrush Transform="{StaticResource ImageTransformResource}" Stretch="Uniform"> <VisualBrush.Visual> <Image Source="/Images/cl1.jpg" /> </VisualBrush.Visual> </VisualBrush> </Rectangle.Fill> </Rectangle> <Rectangle Grid.Column="1" MouseLeftButtonDown="MasterImage_MouseLeftButtonDown" MouseLeftButtonUp="MasterImage_MouseLeftButtonUp" MouseMove="MasterImage_MouseMove" MouseWheel="MasterImage_MouseWheel"> <Rectangle.Fill> <VisualBrush Transform="{StaticResource ImageTransformResource}" Stretch="Uniform"> <VisualBrush.Visual> <Image Source="/Images/cl2.jpg" /> </VisualBrush.Visual> </VisualBrush> </Rectangle.Fill> </Rectangle> </Grid> </Window> using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp120 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private bool m_IsMouseLeftButtonDown; public MainWindow() { InitializeComponent(); } private void MasterImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Rectangle rectangle = sender as Rectangle; if (rectangle == null) return; rectangle.ReleaseMouseCapture(); m_IsMouseLeftButtonDown = false; } private Point m_PreviousMousePoint; private void MasterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Rectangle rectangle = sender as Rectangle; if (rectangle == null) return; rectangle.CaptureMouse(); m_IsMouseLeftButtonDown = true; m_PreviousMousePoint = e.GetPosition(rectangle); } private void MasterImage_MouseMove(object sender, MouseEventArgs e) { Rectangle rectangle = sender as Rectangle; if (rectangle == null) return; if (m_IsMouseLeftButtonDown) DoImageMove(rectangle, e); } private void DoImageMove(Rectangle rectangle, MouseEventArgs e) { //Debug.Assert(e.LeftButton == MouseButtonState.Pressed); if (e.LeftButton != MouseButtonState.Pressed) return; TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup; Debug.Assert(group != null); TranslateTransform transform = group.Children[1] as TranslateTransform; Point position = e.GetPosition(rectangle); transform.X += position.X - m_PreviousMousePoint.X; transform.Y += position.Y - m_PreviousMousePoint.Y; m_PreviousMousePoint = position; } private void MasterImage_MouseWheel(object sender, MouseWheelEventArgs e) { TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup; Debug.Assert(group != null); ScaleTransform transform = group.Children[0] as ScaleTransform; transform.ScaleX += e.Delta * 0.001; transform.ScaleY += e.Delta * 0.001; } } }
Copy from https://www.cnblogs.com/zhangweijia/articles/2480929.html
标签:sender,ZoomOut,Image,System,private,Windows,using,WPF,rectangle From: https://www.cnblogs.com/Fred1987/p/18219231