首页 > 其他分享 >WPF ZoomIn ZoomOut Pan ListBox Image

WPF ZoomIn ZoomOut Pan ListBox Image

时间:2024-07-28 20:55:25浏览次数:16  
标签:1.2 scaler ZoomOut Image System private using void ZoomIn

  <ListBox x:Name="lbx" Grid.Row="1" Grid.Column="0"         
           ItemsSource="{Binding ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
           SelectionChanged="lbx_SelectionChanged"
           SelectedIndex="{Binding ImgIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
      <ListBox.ItemTemplate>
          <DataTemplate>
              <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
                     Width="200" Height="500"/>
          </DataTemplate>
      </ListBox.ItemTemplate>
  </ListBox>
  <Image Grid.Row="1" Grid.Column="1" x:Name="img" ClipToBounds="True"
         Source="{Binding SelectedItem,ElementName=lbx}">
      <Image.RenderTransform>
          <TransformGroup>
              <ScaleTransform x:Name="scaler"/>
              <TranslateTransform x:Name="translater"/>
          </TransformGroup>
      </Image.RenderTransform>
  </Image>

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp226.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:WpfApp226"
        mc:Ignorable="d" WindowState="Maximized" MouseWheel="Window_MouseWheel"
        MouseDown="Window_MouseDown"  MouseUp="Window_MouseUp" MouseMove="Window_MouseMove"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="100"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ToolBar Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
            <Button Content="ZoomIn +" x:Name="ZoomInBtn" Click="ZoomInBtn_Click" Width="100"/>
            <Button Content="ZoomOut -" x:Name="ZoomOutBtn" Click="ZoomOutBtn_Click" Width="100"/>
            <Button Content="Pan" x:Name="panBtn" Click="panBtn_Click"/>
        </ToolBar>
        <ListBox x:Name="lbx" Grid.Row="1" Grid.Column="0"         
                 ItemsSource="{Binding ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 SelectionChanged="lbx_SelectionChanged"
                 SelectedIndex="{Binding ImgIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
                           Width="200" Height="500"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Image Grid.Row="1" Grid.Column="1" x:Name="img" ClipToBounds="True"
               Source="{Binding SelectedItem,ElementName=lbx}">
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaler"/>
                    <TranslateTransform x:Name="translater"/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>
    </Grid>
</Window>




//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;
using System.IO;
using System.Windows.Interop;

namespace WpfApp226
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private ObservableCollection<string> imgsList;
        public ObservableCollection<string> ImgsList
        {
            get
            {
                return imgsList;
            }
            set
            {
                if(value!=imgsList)
                {
                    imgsList = value;
                    OnPropertyChanged(nameof(ImgsList));
                }
            }
        }

        private bool IsPanOpen = false;

        private int imgIdx = 0;
        public int ImgIdx
        {
            get
            {
                return imgIdx;
            }
            set
            {
                if(value!=imgIdx)
                {
                    imgIdx = value;
                    OnPropertyChanged(nameof(ImgIdx));
                }
            }
        }
        private int ImgsCount = 0;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            this.Loaded += MainWindow_Loaded;
            ImgsList = new ObservableCollection<string>(Directory.GetFiles(@"../../Images"));
            ImgsCount = ImgsList.Count;
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            System.Timers.Timer tmr = new System.Timers.Timer();
            tmr.Interval = 100;
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Enabled = true;
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if(++ImgIdx>=ImgsCount)
            {
                ImgIdx = 0;
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
             currentPt=new Point(this.ActualWidth/2, this.ActualHeight/2);
        }

        private void cvs_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if(e.Delta>0)
            {
                scaler.ScaleX *= 1.2;
                scaler.ScaleY *= 1.2;
            }
            else
            {
                scaler.ScaleX /= 1.2;
                scaler.ScaleY /= 1.2;
            }
            scaler.CenterX = e.GetPosition(this).X;
            scaler.CenterY = e.GetPosition(this).Y;
        }

        private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                scaler.ScaleX *= 1.2;
                scaler.ScaleY *= 1.2;
            }
            else
            {
                scaler.ScaleX /= 1.2;
                scaler.ScaleY /= 1.2;
            }
            scaler.CenterX = e.GetPosition(img).X;
            scaler.CenterY = e.GetPosition(img).Y;
        }

        Point currentPt { get; set; }
        bool isMoving = false;

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            currentPt= e.GetPosition(img);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(e.ButtonState == MouseButtonState.Released && e.ChangedButton==MouseButton.Left && isMoving)
            {
                Point newPt= e.GetPosition(img);
                translater.X += scaler.ScaleX * (newPt.X - currentPt.X);
                translater.Y += scaler.ScaleY * (newPt.Y - currentPt.Y);
                isMoving = false;
                IsPanOpen = false;
            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton==MouseButtonState.Pressed)
            {
                isMoving = true;
            } 
        }

        private void ZoomInBtn_Click(object sender, RoutedEventArgs e)
        { 
            scaler.ScaleX *= 1.2;
            scaler.ScaleY *= 1.2;            
            scaler.CenterX = currentPt.X;
            scaler.CenterY = currentPt.Y;
        }

        private void ZoomOutBtn_Click(object sender, RoutedEventArgs e)
        {
            scaler.ScaleX /= 1.2;
            scaler.ScaleY /= 1.2;
            scaler.CenterX = currentPt.X;
            scaler.CenterY = currentPt.Y;
        }

        private void panBtn_Click(object sender, RoutedEventArgs e)
        {
            IsPanOpen = true;
        }

        private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            scaler.ScaleX = 1.0;
            scaler.ScaleY = 1.0;
            scaler.CenterX = 0;
            scaler.CenterY = 0;
            translater.X = 0;
            translater.Y=0;
        }
    }
}

 

标签:1.2,scaler,ZoomOut,Image,System,private,using,void,ZoomIn
From: https://www.cnblogs.com/Fred1987/p/18328861

相关文章

  • ios CCUIImage.m
    ////CCUIImage.h//CCFC_IPHONE////#ifndefCC_UI_IMAGE_H#defineCC_UI_IMAGE_H#ifdef__OBJC__#import"CCConfig.h"#defineCREATE_UIIMAGE(imgPath)[UIImageimageNamed:(imgPath)]#defineCREATE_UIIMAGEVIEW(imgPath)[[......
  • Python,运行Yolo项目,报错AttributeError: ‘ImageDraw‘ object has no attribute ‘te
    Python3.9问题描述:其他电脑已经运行成功的Python,YOLO代码到我电脑上运行报错Traceback(mostrecentcalllast): File"C:\Users\Administrator\Desktop\20240725\识别项目\predict.py",line122,in<module>  frame=np.array(yolo.detect_image(frame)) Fil......
  • tkinter 问题(pyimage10)
    这个回调错误。我尝试使用函数从单独的脚本调用菜单。看起来垃圾收集器正在弄乱菜单图像。我尝试将我的图像移动到字典中并调用按钮/矩形中的页面。你说的没错,Tkinter和Python的垃圾回收机制有时会导致图像无法显示的问题。这通常发生在你创建了一个PhotoImage......
  • ImageJ 用户手册——第三部分(ImageJ扩展)
    ImageJ功能可以通过宏、脚本或插件形式的可加载代码模块进行扩展。ImageJ网站提供了300多个宏、500多个插件和20多个脚本。以下是对这三种类型的ImageJ附加组件的简短描述:宏(Macros)——执行一系列ImageJ命令的最简单方法。ImageJ宏语言是一种类似Java的语言,包含一组控制结构、运算......
  • electron TodoList网页应用打包成linux deb、AppImage应用
    这里用的是windows的wsl的ubuntu环境electron应用打包linux应用需要linux下打包,这里用windows的wsl的ubuntu环境进行操作1)linuxubuntu安装nodejs、electron安装nodejs:sudoaptupdatesudoaptupgrade##快捷安装curl-fsSLhttps://deb.nodesource.com/setup_20.x......
  • RkAiq(Rk Auto Image Quality)模块讲解
    RkAiq模块是一个用于图像质量优化和处理的库,通常用于图像信号处理(ISP)框架中,特别是在摄像头应用中。RkAiq代表"RockchipAIImageQuality",它由Rockchip开发,旨在利用人工智能和先进的算法来提升图像质量。概述ISP30包含了一系列的图像处理算法模块,主要包括:暗电流矫正、坏点矫......
  • 单目三维内容生成系列—Zero-1-to-3: Zero-shot One Image to 3D Object
    文章目录1.概述2.背景介绍3.方法3.1学习控制相机视角3.2视角条件扩散3.33D重建3.4数据集4.实验评测4.1任务4.2基线4.3基准和指标4.4新视角合成结果4.53D重建结果4.6从文本到图像到3D5.讨论1.概述我们介绍了Zero-1-to-3框架,该框架仅需一张RGB图像即......
  • 如何免费提取PDF里的图片-pdfimages使用教程
    写在前面本随笔是非常菜的菜鸡写的。如有问题请及时提出。可以联系:[email protected]:https://github.com/WindDevil(目前啥也没有动机由于WPS的提取图片需要收费,作为穷鬼的我直接在bing,键入opensourcesoftwaregetpictureformpdf.找到这个项目:VR51/Batch-PDF......
  • 解决el-image预览总是从第一页开始问题
    <el-imagev-for="(image,index)inform.imgUrl":key="index":src="image"class="image":preview-src-list="privew......
  • ImageEn v10.2.0 for Delphi 5-12 Crack
    ImageEnv10.2.0forDelphi5-12CrackKeyFeaturesofImageEn:Extensivecomponentsuiteforimageediting,display,andanalysisCompatiblewithDelphiandC++Builder5-7,2005-2010,XE-XE8,10,11,12,and.NET2.0ornewerSup......