首页 > 其他分享 >WPF Image ZoomIn ZoomOut Pan/Move Rotate

WPF Image ZoomIn ZoomOut Pan/Move Rotate

时间:2024-06-14 21:13:24浏览次数:18  
标签:Rotate sender scaler ZoomOut Image System Windows using void

<Window x:Class="WpfApp162.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:WpfApp162"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <ToolBar Grid.Row="0">
            <Button Content="Open"  x:Name="openBtn" Click="openBtn_Click"/>
            <Button Content="Rotate 45d" x:Name="rotateBtn" Click="rotateBtn_Click"/>
        </ToolBar>
        <Grid x:Name="gd" Grid.Row="1" Background="Transparent" MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" 
          MouseUp="Grid_MouseUp" MouseWheel="Grid_MouseWheel" ClipToBounds="True">
            <Image x:Name="img" ClipToBounds="True">
                <Image.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="scaler"/>
                        <TranslateTransform x:Name="translater"/>
                        <RotateTransform x:Name="rotater"/>
                    </TransformGroup>
                </Image.RenderTransform>
            </Image>
        </Grid>
    </Grid>
</Window>


using Microsoft.Win32;
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;

namespace WpfApp162
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Point oldPoint { get; set; }
        public bool isMoving { get; set; } = false;
        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            oldPoint = e.GetPosition(gd);
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            isMoving = true;
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(isMoving && e.ChangedButton==MouseButton.Left && e.ButtonState == MouseButtonState.Released)
            {
                Point newPoint = e.GetPosition(gd);
                translater.X += newPoint.X - oldPoint.X;
                translater.Y += newPoint.Y - oldPoint.Y;
                isMoving = false;
            }
        }

        private void Grid_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(gd).X;
            scaler.CenterY = e.GetPosition(gd).Y;
        }

        private void openBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog()==true)
            {
                scaler.ScaleX = 1.0;
                scaler.ScaleY=1.0;
                scaler.CenterX = 0;
                scaler.CenterY = 0;
rotater.Angle = 0; string imgUrl=ofd.FileName; BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute); bmi.EndInit(); img.Source= bmi; } } private void rotateBtn_Click(object sender, RoutedEventArgs e) { rotater.Angle += 45; rotater.CenterX = this.ActualWidth / 2; rotater.CenterY = this.ActualHeight / 2; } } }

 

 

 

 

 

 

 

 

标签:Rotate,sender,scaler,ZoomOut,Image,System,Windows,using,void
From: https://www.cnblogs.com/Fred1987/p/18248635

相关文章

  • delphi Image32 图片转换成SVG
    image32中有2种算法转换图像为svg,一种是按透明度计算找边缘,另一种是分析像素梯度找边缘,demo代码整理后如下:unituFrmImageToSVG;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.......
  • delphi Image32 SVG图形查看器
    DELPHI 中没有SVG显示组件,需要用到第三方组件,高版本可以使用skia(但必须带上skia.dll).最新版Image32修改了很多,SVGIconImageList 也因此换成了Image32做为基础库,安装了 SVGIconImageList 就可以不用再单独安装Image32了(基本上是绿色不用安装包,直接引用就行)。unituFrmSVGSh......
  • delphi Image32 图形处理 图层
    图形图层处理是Image32的主要功能,矢量图形,分层类似Photoshop看人图层,直接上代码效果。unituFrmLayer;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Types,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,V......
  • delphi Image32 路径
    用Image32的理由之一,也是路径这块做得比delphi(FMX)自带的要好,skia中支持svg,但对路径处理功能不够强大。VCL只能使用第三方库。VCL如果要支持SVG,只有Image32好点,SVGIconImageList 第三方库也使用Image32.  unituFrmPaths;interfaceusesWinapi.Windows,Winapi.M......
  • 【高光谱遥感分类论文解读1】Hyperspectral Image Classification Using Group-Aware
    目录一、论文基本信息二、研究背景三、研究方法1.GAHT总体框架2.GPE模块3.Transformer编码模块四、实验本文是博主对原论文的解读,仅代表博主个人观点,欢迎在评论区和我交流~其中,本博文中的图片和公式均来源于原论文,如需进一步了解,请查看原论文。一、论文基本信息......
  • 升级babel7后,直接引用element-ui中没有暴露出来的组件image-viewer.vue导致的打包错误
    问题&解决方案升级babel7后,原先代码中像这样直接引用element-ui组件的地方,出现了报错Moduleparsefailed:Unexpectedtoken(1:0)Youmayneedanappropriateloadertohandlethisfiletype.经过一番排查,我发现问题出在element-ui并未直接暴露该组件,导致直接引用时......
  • Summary:《Adversarial Machine Learning in Image Classification: A Survey Towards
    Note“TaxonomyofAdversarialImages”(Machado等,2023,p.5)(pdf)扰动范围(PerturbationScope):个体扰动(Individual-scopedperturbations):为每个输入图像单独生成的扰动。通用扰动(Universal-scopedperturbations):独立于任何输入样本生成的扰动,可应用于任何合......
  • DISM(Deployment Image Servicing and Management)和wimlib虽然都可以用来处理Windows映
    DISM(DeploymentImageServicingandManagement)和wimlib都是用于Windows系统的映像管理工具,它们可以用来处理Windows映像文件(.wim文件),但在功能和使用上有一些不同点。下面是它们的比较:DISM(DeploymentImageServicingandManagement)内置工具:DISM是Windows操作系统......
  • Image 32 动画演示1
    Image32自带的Demo,添加一些注解。 unituFrmAnimation;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Classes,System.Variants,System.Math,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ExtCtrls,//......
  • Image32 动画演示2
    Image32自带的Demo,添加一些注解。unituFrmAnimation2;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ExtCtrls,System.Math,Img3......