WPF Image open ZoomIn ZoomOut reset

//xaml
<Window x:Class="WpfApp94.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:WpfApp94"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition  />
        </Grid.RowDefinitions>
        <ToolBar Grid.Row="0">
            <Button Command="{Binding OpenCmd}" Content="Open"/>
            <Button Command="{Binding ZoomInCmd}" Content="ZoomIn"/>
            <Button Command="{Binding ZoomOutCmd}" Content="ZoomOut"/>
            <Button Command="{Binding ZoomNormalCmd}" Content="ZoomNormal"/>
        </ToolBar>
        <ScrollViewer Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Image Source="{Binding ImgPath}" Stretch="None">
                <Image.LayoutTransform >
                    <ScaleTransform ScaleX="{Binding Zoom}"
                                    ScaleY="{Binding Zoom}"/>
                </Image.LayoutTransform>
            </Image>
        </ScrollViewer>
    </Grid>
</Window>


//cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security;
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 WpfApp94
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainVM();
            this.DataContext = vm;
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }
        
        public DelCmd OpenCmd { get; set; }
        public DelCmd ZoomInCmd { get; set; }
        public DelCmd ZoomOutCmd { get; set; }
        public DelCmd ZoomNormalCmd { get; set; }
                

        private string imgPath; 
        public string ImgPath
        {
            get
            {
                return imgPath;
            }
            set
            {
                if (value != imgPath)
                {
                    imgPath = value;
                    OnPropertyChanged(nameof(ImgPath));
                }
            }
        }

        private double zoom = 1.0;
        public double Zoom
        {
            get { return zoom; }
            set
            {
                if (value != zoom)
                {
                    zoom = value;
                    OnPropertyChanged("Zoom");
                }
            }
        }

        public MainVM()
        {
            OpenCmd = new DelCmd(OpenCmdExecuted);
            ZoomInCmd = new DelCmd(ZoomInCmdExecuted, ZoomInCmdCanExecute);
            ZoomOutCmd = new DelCmd(ZoomOutCmdExecuted, ZoomOutCmdCanExecute);
            ZoomNormalCmd = new DelCmd(ZoomNormalCmdExecuted, ZoomNormalCmdCanExecute);
        }

        private bool ZoomNormalCmdCanExecute(object obj)
        {
            return !string.IsNullOrWhiteSpace(ImgPath);
        }

        private void ZoomNormalCmdExecuted(object obj)
        { 
            Zoom = 1;
        }

        private bool ZoomOutCmdCanExecute(object obj)
        {
            return !string.IsNullOrWhiteSpace(ImgPath);
        }

        private void ZoomOutCmdExecuted(object obj)
        { 
            Zoom /= 1.2;
        }

        private bool ZoomInCmdCanExecute(object obj)
        {
            return !string.IsNullOrWhiteSpace(ImgPath);
        }

        private void ZoomInCmdExecuted(object obj)
        { 
            Zoom *= 1.2;
        }

        private void OpenCmdExecuted(object obj)
        {
            var dlg = new OpenFileDialog
            {
                Filter = "Image Files|*.jpg;*.png;*.bmp;*.gif"
            };
            if (dlg.ShowDialog() == true)
            {
                ImgPath = dlg.FileName; 
            }
        }
    }

    public class DelCmd : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Predicate<object> _canExecute;

        public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue)
        {
            _execute = executeValue;
            _canExecute = canExecuteValue;
        }

        public DelCmd(Action<object> executeValue) : this(executeValue, null) { }

        public event EventHandler CanExecuteChanged;
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler?.Invoke(this, EventArgs.Empty);
            }
        }

        public bool CanExecute(object parameter)
        {
            if (parameter == null)
            {
                return true;
            }
            return _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
     
}

 

 

 

posted @ 2024-05-05 17:05  FredGrit  阅读(11)  评论(0编辑  收藏  举报