//xaml <Window x:Class="WpfApp123.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:WpfApp123" mc:Ignorable="d" WindowState="Maximized" Title="MainWindow" Height="450" Width="800"> <Grid> <Image x:Name="img" Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MouseWheel="img_MouseWheel"> </Image> </Grid> </Window> //xaml.cs using System; using System.Collections.Generic; using System.ComponentModel; 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 WpfApp123 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window,INotifyPropertyChanged { #region Properties private string imgUrl; public string ImgUrl { get { return imgUrl; } set { imgUrl = value; OnPropertyChanged(nameof(ImgUrl)); } } private double zoomLevel=1.0; public double ZoomLevel { get { return zoomLevel; } set { zoomLevel = value; OnPropertyChanged(nameof(ZoomLevel)); } } #endregion public MainWindow() { InitializeComponent(); this.DataContext = this; ImgUrl = @"..\..\Images\cl.jpg"; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName) { var handler = PropertyChanged; if(handler!=null) { handler?.Invoke(this, new PropertyChangedEventArgs(propName)); } } private void img_MouseWheel(object sender, MouseWheelEventArgs e) { img.RenderTransform = null; var position = e.MouseDevice.GetPosition(img); if (e.Delta>0) { ZoomLevel *= 1.2; } else { ZoomLevel /= 1.2; } img.RenderTransform = new ScaleTransform(ZoomLevel, ZoomLevel, position.X, position.Y); } } }
Amplify/enlarge left eye
标签:via,MouseWheel,center,img,Windows,System,ZoomLevel,using,public From: https://www.cnblogs.com/Fred1987/p/18220665