【WPF】对Image元素进行缩放平移等操作

元素布局

xml 复制代码
        <Border Grid.Row="1" Name="border" ClipToBounds="True" Margin="10,10,10,10">
            <Image Name="image" Visibility="Visible" Margin="3,3,3,3" Grid.Column="1" Source="{Binding SourceImage}" HorizontalAlignment="Center" VerticalAlignment="Center">
            </Image>
        </Border>

1.平移

1.1 定义私有变量记录相关初始位置

cs 复制代码
        /// <summary>
        /// 图片的起始偏移位置
        /// </summary>
        private Point origin;  
        /// <summary>
        /// 鼠标相对Border的起始位置
        /// </summary>
        private Point start; 

1.2 绑定 鼠标左键按下鼠标左键抬起鼠标移动事件

复制代码
        public DialogImageWindow()
        {
            InitializeComponent();
            image.MouseLeftButtonDown += image_MouseLeftButtonDown;
            image.MouseLeftButtonUp += image_MouseLeftButtonUp;
            image.MouseMove += image_MouseMove;
        }

1.3 鼠标左键按下

  1. 通过CaptureMouse将鼠标捕获到指定的元素image
  2. 通过GetPosition获取鼠标相对border位置
  3. 记录image当前偏移位置
cs 复制代码
        private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (image.IsMouseCaptured) return;
            image.CaptureMouse();

            start = e.GetPosition(border);
            origin.X = image.RenderTransform.Value.OffsetX;
            origin.Y = image.RenderTransform.Value.OffsetY;
        }

1.4 鼠标移动

  1. 通过GetPosition获取当前鼠标相对border位置
  2. 计算鼠标相对初始位置移动的像素差值
  3. 将像素差值累计到初始偏移量上
cs 复制代码
 private void image_MouseMove(object sender, MouseEventArgs e)
        {
            if (!image.IsMouseCaptured) return;
            Point p = e.MouseDevice.GetPosition(border);

            Matrix m = image.RenderTransform.Value;
            m.OffsetX = origin.X + (p.X - start.X);
            m.OffsetY = origin.Y + (p.Y - start.Y);

            image.RenderTransform = new MatrixTransform(m);
        }

1.5 鼠标左键抬起

  1. 通过ReleaseMouseCapture释放鼠标捕获
cs 复制代码
private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (image.IsMouseCaptured)
            {
                image.ReleaseMouseCapture();
            }
        }

2. 缩放

2.1 绑定 鼠标滚轮事件

cs 复制代码
        public DialogImageWindow()
        {
            InitializeComponent();
            MouseWheel += MainWindow_MouseWheel;
        }

2.1

  1. 通过GetPosition获取鼠标相对image的位置,该位置作为缩放中心点
  2. 根据Delta参数来决定是在当前放大系数上放大(1.1倍)还是缩小(1.1倍).
cs 复制代码
 private void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Point p = e.MouseDevice.GetPosition(image);

            Matrix m = image.RenderTransform.Value;
            if (e.Delta > 0)
                m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
            else
                m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);

            image.RenderTransform = new MatrixTransform(m);
        }

3. 还原

cs 复制代码
private void b1_Click(object sender, RoutedEventArgs e)
        {
            image.RenderTransform = new ScaleTransform(1.0,1.0);
            image.RenderTransform = new TranslateTransform(0,0);
        }
相关推荐
软泡芙20 小时前
【WPF 】MVVM 设计模式在 WPF 中的实战应用
设计模式·wpf
张小俊_21 小时前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
七夜zippoe2 天前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang2 天前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com2 天前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf
武藤一雄3 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
炸炸鱼.3 天前
ELK 企业级日志分析系统完整部署手册
elk·wpf
Mr_pyx4 天前
微服务可观测性实战:分布式链路追踪从入门到精通
wpf
c#上位机5 天前
wpf附加事件
wpf
玖笙&5 天前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio