首页 > 其他分享 >WPF Image ZoomIn ZoomOut via MouseWheel

WPF Image ZoomIn ZoomOut via MouseWheel

时间:2024-05-21 17:42:27浏览次数:8  
标签:via MouseWheel string ZoomOut System private Windows using public

//xaml
<Window x:Class="WpfApp104.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:WpfApp104"
        mc:Ignorable="d" WindowState="Maximized"
        Title="{Binding ScaleValueStr}" Height="450" Width="800">
    <Grid> 
        <Canvas  x:Name="imgContainer">
            <Canvas.LayoutTransform>
                <ScaleTransform ScaleX="{Binding ScaleValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                ScaleY="{Binding ScaleValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
            </Canvas.LayoutTransform>
            <Image Source="{Binding ImgSource,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        MouseRightButtonDown="imgContainer_MouseRightButtonDown" Stretch="Uniform"
        MouseLeftButtonDown="Image_MouseLeftButtonDown"
        MouseWheel="imgContainer_MouseWheel"  /> 
        </Canvas>
        <TextBlock Text="{Binding ScaleValueStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontSize="30" />
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
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 WpfApp104
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    { 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

        private double scaleValue = 1.0;
        public double ScaleValue
        {
            get
            {
                return scaleValue;
            }
            set
            {
                if (scaleValue != value)
                {
                    scaleValue = value;
                    OnPropertyChanged(nameof(ScaleValue));
                    ScaleValueStr = Math.Round(ScaleValue, 2).ToString("0.00"); 
                }
            }
        }

        private string scaleValueStr = "1.00";
        public string ScaleValueStr
        {
            get
            {
                return scaleValueStr;
            }
            set
            {
                if(value!= scaleValueStr) 
                {
                    scaleValueStr = value;
                    OnPropertyChanged(nameof(ScaleValueStr));
                }
            }
        }

        private string imgSource = string.Empty;
        public string ImgSource
        {
            get
            {
                return imgSource;
            }
            set
            {
                if(value!= imgSource) 
                {
                    imgSource = value;
                    OnPropertyChanged(nameof(ImgSource));
                }
            }
        }

        private static List<string> ImgsList = new List<string>();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext= this;
            InitImgsList();
            ImgSource = ImgsList[0];
        }

        private void InitImgsList()
        {
            string fullDir = System.IO.Path.GetFullPath(@"..\..\Images");
            ImgsList.AddRange(Directory.GetFiles(fullDir));
        }

        private void imgContainer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if(e.Delta>0)
            {
                ScaleValue *= 1.2;
            }
            else
            {
                ScaleValue /= 1.2;
            } 
        }

        private int currentIndex = 0;
        private void imgContainer_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        { 
            if (currentIndex<0 || currentIndex+1>=ImgsList.Count)
            {
                currentIndex = 0; 
            }
            ImgSource = ImgsList[++currentIndex]; 
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        { 
            if (currentIndex-1<0)
            {
                currentIndex=ImgsList.Count-1;
            }
            ImgSource = ImgsList[--currentIndex]; 
        }
    }
}

 

 

 

标签:via,MouseWheel,string,ZoomOut,System,private,Windows,using,public
From: https://www.cnblogs.com/Fred1987/p/18204621

相关文章

  • [Paper Reading] BEVFormer: Learning Bird’s-Eye-View Representation from Multi-C
    BEVFormer:LearningBird’s-Eye-ViewRepresentationfromMulti-CameraImagesviaSpatiotemporalTransformerslink时间:22.07机构:NanjingUniversity&&ShanghaiAILaboratoryTL;DR利用Transformer的Attention机制融合时空特征信息,在nuScenes测试集上达到SOTA精度,同时......
  • RAG Project with Ollama and LangChain via Gradio Interface
    RAGProjectwithOllamaandLangChainviaGradioInterfacehttps://github.com/fanqingsong/rag-ollama-langchainThisrepositoryhoststheimplementationofaRetrieval-AugmentedGeneration(RAG)projectleveragingthecapabilitiesofOllamatorunopen-so......
  • 2024CVPR_Low-light Image Enhancement via CLIP-Fourier Guided Wavelet Diffusion(C
    一、Motivation1、单模态监督问题:大多数方法往往只考虑从图像层面监督增强过程,而忽略了图像的详细重建和多模态语义对特征空间的指导作用。这种单模态监督导致不确定区域的次优重建和较差的局部结构,导致视觉结果不理想的出现。------》扩散模型缺乏有效性约束,容易出现多种生成效......
  • WPF Image open ZoomIn ZoomOut reset
    //xaml<Windowx: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.mic......
  • WPF MVVM Datagrid Selected Multiple items via behavior interaction.trigger,event
    1.Install Microsoft.Xaml.Behaviors.WpffromNuget;2.Addbehaviorreferenceinxamlxmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"3.Passmethodtomvvmviabehavior,interaction,trigger,eventname,TargetObject,MethodNameinxaml......
  • WPF pass event method to viewmodel via Interaction:CallMethodAction,TargetObject
    <Windowx:Class="WpfApp71.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • [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模块,细节如图描......
  • 论文笔记-Modeling of dynamic characteristic of particle in transient gas–solid
    对象:气固两相流+数值模拟方法:RCNN=RNN+CNN目标:学习颗粒流的时间和空间不均匀性并预测颗粒动态关注特征:关注颗粒不均匀性对颗粒动力学的独特影响,旨在提出一种基于机器学习的方法来建模颗粒不均匀性和颗粒动力学之间的映射结果:R-CNN模型的预测精度用1-9个时间步长(即1-9ms)的各......
  • Enhancing ID and Text Fusion via Alternative Training in Session-based Recommend
    目录概MotivationAlterRec代码LiJ.,HanH.,ChenZ.,ShomerH.,JinW.,JavariA.andTangJ.EnhancingIDandtextfusionviaalternativetraininginsession-basedrecommendation.2024.概作者“发现”多模态推荐中ID和文本模态的结合做的并不好,于是乎提出......
  • 开源向量数据库比较:Chroma, Milvus, Faiss,Weaviate
    语义搜索和检索增强生成(RAG)正在彻底改变我们的在线交互方式。实现这些突破性进展的支柱就是向量数据库。选择正确的向量数据库能是一项艰巨的任务。本文为你提供四个重要的开源向量数据库之间的全面比较,希望你能够选择出最符合自己特定需求的数据库。什么是向量数据库?向量数......