首页 > 其他分享 >WPF Image open ZoomIn ZoomOut reset

WPF Image open ZoomIn ZoomOut reset

时间:2024-05-05 17:11:21浏览次数:28  
标签:reset ZoomOut Image System private Windows using public DelCmd

//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);
        }
    }
     
}

 

 

 

标签:reset,ZoomOut,Image,System,private,Windows,using,public,DelCmd
From: https://www.cnblogs.com/Fred1987/p/18173650

相关文章

  • BufferedImage内存泄漏和溢出问题
    java的ImageIO处理图片在使用Thumbnailator时出现了OOM问题,但是其使用方法只有一行代码,无法针对其内部使用的对象进行资源释放,所以使用原生的Java类库中ImageIO来处理图片。关键有三个类:ImageIO、BufferedImage、GraphicsImageIO类包含两个静态方法:read()和write(),通过这两......
  • 最小化安装 MSVC ( 可用于 graalvm native-image )
    前言自从接触了native-image,就想把所有Java项目全用native-image编译一遍,谁不喜欢exe呢......
  • [Paper Reading] DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D
    名称DETR3D:3DObjectDetectionfromMulti-viewImagesvia3D-to-2DQueries时间:21.10机构:mit/CMU/StanfordTL;DR一种利用Transformer做E2E的3D目标检测方法,在nuScenes自动驾驶数据集上取得很好效果。Method主要创新点在于2D-to-3DFeatureTransforms模块,细节如图描......
  • AWS S3 Lambda Python脚本函数执行时报错AttributeError: module ‘PIL‘ has no attr
    背景代码示例如下importPILdefadd_image(self,tag,img,step):summary=Summary()bio=BytesIO()iftype(img)==str:img=PIL.Image.open(img)eliftype(img)==PIL.Image.Image:passelse:img=scipy.misc.......
  • C#ManualResetEvent 在线程中的使用
    ManualResetEvent用于表示线程同步事件,可以使得线程等待信号发射之后才继续执行下一步,否则一直处于等待状态中。ManualResetEvent的常用方法构造函数ManualResetEvent(bool);ManualResetEventmanualResetEvent=newManualResetEvent(false);//false将初始状态设......
  • Mysql 密码报错 You must reset your password ... 和 Your password does N
    如果MySQL数据库用户的密码设置过于简单,数据库在用户登录后会提示重置密码,并且不接受简单的密码。提示需要重置密码:ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatementbeforeexecutingthisstatement.Mysql数据库版本:5.7.1操作系统:CentOS7这......
  • ImageJ软件使用教程(三):目标计数
    目录多点工具法阀值分割法二值化填充分割自动计数显示结果总结参考资料本文以钢筋计数为例,讲解一下如何使用ImageJ软件进行计数,这里只介绍两种方法:多点工具法阀值分割法钢筋计数是我接触的第一个视觉项目,虽然项目最后不了了之,但作为我机器视觉的开荒项目还是很有纪念意义。......
  • vscode使用PasteImage插入图片
    vscode使用PasteImage插入图片需求在vscode中写Markdown文件,经常需要插入图片,使用插件PasteImage进行简单配置后,就可以方便插入图片并自动存放到相应路径的文件夹中安装及配置安装从扩展商店搜索PasteImage并安装即可配置vscode设置中搜索PasteImage,找到PasteImage:......
  • Windows Assessment and Deployment Kit(ADK)中的 DISM(Deployment Image Servicing and
    WindowsAssessmentandDeploymentKit(ADK)中的DISM(DeploymentImageServicingandManagement)工具是用于管理Windows映像(WIM)和虚拟硬盘(VHD)文件的命令行工具。DISM提供了许多功能和参数,其中一些可能不太常见或被称为“隐藏参数”。这些隐藏参数通常不在DISM命令的文档......
  • Git reset 中四大模式:soft、mixed、hard、keep 的区别
    Gitreset中四大模式:soft、mixed、hard、keep的区别目录Gitreset中四大模式:soft、mixed、hard、keep的区别gitreset--soft(常用)gitreset--mixed(默认)gitreset--hard(慎用)gitreset--keep(吃灰)参考工作区暂存区本地版本库soft保持所有保持回退mixed保......