WPF Image ZoomIn ZoomOut Pan/Move Rotate
<Window x:Class="WpfApp162.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:WpfApp162" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid > <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition/> </Grid.RowDefinitions> <ToolBar Grid.Row="0"> <Button Content="Open" x:Name="openBtn" Click="openBtn_Click"/> <Button Content="Rotate 45d" x:Name="rotateBtn" Click="rotateBtn_Click"/> </ToolBar> <Grid x:Name="gd" Grid.Row="1" Background="Transparent" MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp" MouseWheel="Grid_MouseWheel" ClipToBounds="True"> <Image x:Name="img" ClipToBounds="True"> <Image.RenderTransform> <TransformGroup> <ScaleTransform x:Name="scaler"/> <TranslateTransform x:Name="translater"/> <RotateTransform x:Name="rotater"/> </TransformGroup> </Image.RenderTransform> </Image> </Grid> </Grid> </Window> using Microsoft.Win32; using System; using System.Collections.Generic; 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 WpfApp162 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } Point oldPoint { get; set; } public bool isMoving { get; set; } = false; private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { oldPoint = e.GetPosition(gd); } private void Grid_MouseMove(object sender, MouseEventArgs e) { isMoving = true; } private void Grid_MouseUp(object sender, MouseButtonEventArgs e) { if(isMoving && e.ChangedButton==MouseButton.Left && e.ButtonState == MouseButtonState.Released) { Point newPoint = e.GetPosition(gd); translater.X += newPoint.X - oldPoint.X; translater.Y += newPoint.Y - oldPoint.Y; isMoving = false; } } private void Grid_MouseWheel(object sender, MouseWheelEventArgs e) { if(e.Delta>0) { scaler.ScaleX *= 1.2; scaler.ScaleY *= 1.2; } else { scaler.ScaleX /= 1.2; scaler.ScaleY /= 1.2; } scaler.CenterX = e.GetPosition(gd).X; scaler.CenterY = e.GetPosition(gd).Y; } private void openBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog()==true) { scaler.ScaleX = 1.0; scaler.ScaleY=1.0; scaler.CenterX = 0; scaler.CenterY = 0;
rotater.Angle = 0; string imgUrl=ofd.FileName; BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute); bmi.EndInit(); img.Source= bmi; } } private void rotateBtn_Click(object sender, RoutedEventArgs e) { rotater.Angle += 45; rotater.CenterX = this.ActualWidth / 2; rotater.CenterY = this.ActualHeight / 2; } } }