//xaml
<Window x:Class="WpfApp109.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:WpfApp109"
mc:Ignorable="d" WindowState="Maximized"
Title="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800">
<Grid x:Name="gd" >
<Image Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" MouseWheel="gd_MouseWheel">
<Image.RenderTransform>
<ScaleTransform ScaleX="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
ScaleY="{Binding ZoomLevel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
</ScaleTransform>
</Image.RenderTransform>
</Image>
</Grid>
</Window>
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;
using System.IO;
using System.ComponentModel;
namespace WpfApp109
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window,INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
InitContent();
this.DataContext= this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var handler = PropertyChanged;
if(handler!=null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private string imgUrl;
public string ImgUrl
{
get
{
return imgUrl;
}
set
{
if (value != imgUrl)
{
imgUrl = value;
OnPropertyChanged(nameof(ImgUrl));
}
}
}
private double zoomLevel = 1.0;
public double ZoomLevel
{
get
{
return zoomLevel;
}
set
{
if(value!=zoomLevel)
{
zoomLevel = value;
OnPropertyChanged(nameof(ZoomLevel));
}
}
}
private void InitContent()
{
string dir= @"..\..\Images\cl14.jpg";
string fullDir = System.IO.Path.GetFullPath(dir);
if(File.Exists(fullDir))
{
ImgUrl = fullDir;
}
}
private void gd_MouseWheel(object sender, MouseWheelEventArgs e)
{
if(e.Delta>0)
{
ZoomLevel *= 1.2;
}
else
{
ZoomLevel /= 1.2;
}
}
}
}
![]()
![]()
![]()
![]()