WPF Canvas 中移动和缩放图片

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

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 WpfApplication1

{

   /// <summary>

   /// MainUC.xaml 的交互逻辑

   /// </summary>

   public partial class MainUC : UserControl

   {

       private Point oldPoint = new Point();

       public MainUC()

       {

           InitializeComponent();

       }


       private void Image_MouseDown(object sender, MouseButtonEventArgs e)

       {

           Image image = sender as Image;

           oldPoint = e.GetPosition(this);

           image.CaptureMouse();//捕获鼠标

       }


       private void Image_MouseUp(object sender, MouseButtonEventArgs e)

       {

           Image image = sender as Image;

           image.ReleaseMouseCapture();//释放鼠标

       }


       private void Image_MouseMove(object sender, MouseEventArgs e)

       {

           if (e.LeftButton == MouseButtonState.Pressed)

           {

               Point point = e.GetPosition(this);

               Image image = sender as Image;

               double left = (double)image.GetValue(Canvas.LeftProperty);

               double top = (double)image.GetValue(Canvas.TopProperty);

                left =left+ Math.Round(point.X - oldPoint.X);

                top = top + Math.Round(point.Y - oldPoint.Y);


               image.SetValue(Canvas.LeftProperty, left);

               image.SetValue(Canvas.TopProperty, top);

               oldPoint = e.GetPosition(this);

           }

       }


       private void Image_MouseWheel(object sender, MouseWheelEventArgs e)

       {

           Image image = sender as Image;

           Point locationPoint=new Point();

           Vector deltaLocate=new Vector();

           //滚轮缩放

           locationPoint.X = Canvas.GetLeft(image);  //获得Image缩放前相对Canvas的左上角坐标

           locationPoint.Y = Canvas.GetTop(image);

           deltaLocate.X = image.ActualWidth;   //记下Image缩放前的宽和高

           deltaLocate.Y = image.ActualHeight;






           if (e.Delta > 0)     //滚轮向上,放大

           {

               image.Width = deltaLocate.X * 1.1;

               image.Height = deltaLocate.Y * 1.1;

           }

           if (e.Delta < 0)     //滚轮向下,缩小

           {

               image.Width = deltaLocate.X / 1.1;

               image.Height = deltaLocate.Y / 1.1;

           }


           deltaLocate.X -= image.Width;   //计算image左上角坐标的改变量

           deltaLocate.Y -= image.Height;

           deltaLocate.X /=2;

           deltaLocate.Y /= 2;


           //重新设置Viewbox相对于Canvas的左上角坐标,使得缩放是相对于Viewbox中心进行的

           image.SetValue(Canvas.LeftProperty, locationPoint.X + deltaLocate.X);

           image.SetValue(Canvas.TopProperty, locationPoint.Y + deltaLocate.Y);


       }

   }

}


知识共享许可协议
《WPF Canvas 中移动和缩放图片》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     165.95ms