经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#控件picturebox实现图像拖拽和缩放
来源:jb51  时间:2018/9/25 19:01:26  对本文有异议

本文实例为大家分享了C# picturebox实现图像拖拽和缩放的具体代码,供大家参考,具体内容如下

1.核心步骤:

①新建Point类型全局变量mouseDownPoint,记录拖拽过程中鼠标位置;

②MouseDown事件记录Cursor位置;

③MouseMove事件计算移动矢量,并更新pictureBox1.Location。

代码:

  1. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  2.   {
  3.    if (e.Button == MouseButtons.Left)
  4.    {
  5.     mouseDownPoint.= Cursor.Position.X; //记录鼠标左键按下时位置
  6.     mouseDownPoint.= Cursor.Position.Y;    
  7.     isMove = true;
  8.     pictureBox1.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点
  9.    }
  10.   }
  11.  
  12.   private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  13.   {
  14.    if (e.Button == MouseButtons.Left)
  15.    {
  16.     isMove = false;    
  17.    }
  18.   }
  19.  
  20.   private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  21.   {
  22.    pictureBox1.Focus(); //鼠标在picturebox上时才有焦点,此时可以缩放
  23.    if (isMove)
  24.    {
  25.     int x, y;   //新的pictureBox1.Location(x,y)
  26.     int moveX, moveY; //X方向,Y方向移动大小。
  27.     moveX = Cursor.Position.- mouseDownPoint.X;
  28.     moveY = Cursor.Position.- mouseDownPoint.Y;
  29.     x = pictureBox1.Location.+ moveX;
  30.     y = pictureBox1.Location.+ moveY;    
  31.     pictureBox1.Location = new Point(x, y);
  32.     mouseDownPoint.= Cursor.Position.X;
  33.     mouseDownPoint.= Cursor.Position.Y;    
  34.    }
  35.   }
  36.   
  37.   private void panel2_MouseDown(object sender, MouseEventArgs e)
  38.   {
  39.    if (e.Button == MouseButtons.Left)
  40.    {
  41.     mouseDownPoint.= Cursor.Position.X; //记录鼠标左键按下时位置
  42.     mouseDownPoint.= Cursor.Position.Y;
  43.     isMove = true;
  44.    }
  45.   }
  46.  
  47.   private void panel2_MouseUp(object sender, MouseEventArgs e)
  48.   {
  49.    if (e.Button == MouseButtons.Left)
  50.    {
  51.     isMove = false;
  52.    }
  53.   }
  54.  
  55.   private void panel2_MouseMove(object sender, MouseEventArgs e)
  56.   {
  57.    panel2.Focus(); //鼠标不在picturebox上时焦点给别的控件,此时无法缩放   
  58.    if (isMove)
  59.    {
  60.     int x, y;   //新的pictureBox1.Location(x,y)
  61.     int moveX, moveY; //X方向,Y方向移动大小。
  62.     moveX = Cursor.Position.- mouseDownPoint.X;
  63.     moveY = Cursor.Position.- mouseDownPoint.Y;
  64.     x = pictureBox1.Location.+ moveX;
  65.     y = pictureBox1.Location.+ moveY;
  66.     pictureBox1.Location = new Point(x, y);
  67.     mouseDownPoint.= Cursor.Position.X;
  68.     mouseDownPoint.= Cursor.Position.Y;
  69.    }
  70.   }

2.图像缩放

核心思想:利用picturebox的zoom模式,根据图像显示大小更改picturebox大小,记录鼠标位置补偿缩放位移,实现锚点缩放,即以鼠标位置为中心进行缩放。
zoomstep --- 自己定义滚轮滑动缩放大小

代码:

  1. //实现锚点缩放(以鼠标所指位置为中心缩放);
  2.   //步骤:
  3.   //①先改picturebox长宽,长宽改变量一样;
  4.   //②获取缩放后picturebox中实际显示图像的长宽,这里长宽是不一样的;
  5.   //③将picturebox的长宽设置为显示图像的长宽;
  6.   //④补偿picturebox因缩放产生的位移,实现锚点缩放。
  7.   // 注释:为啥要②③步?由于zoom模式的机制,把picturebox背景设为黑就知道为啥了。
  8.   //这里需要获取zoom模式下picturebox所显示图像的大小信息,添加 using System.Reflection;
  9.   //pictureBox1_MouseWheel事件没找到。。。手动添加,别忘在Form1.Designer.cs的“Windows 窗体设计器生成的代码”里加入:  
  10.   //this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel)。
  11.   private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  12.   {
  13.    int x = e.Location.X;
  14.    int y = e.Location.Y;
  15.    int ow = pictureBox1.Width;
  16.    int oh = pictureBox1.Height;   
  17.    int VX, VY;  //因缩放产生的位移矢量
  18.    if (e.Delta > 0) //放大
  19.    {
  20.     //第①步
  21.     pictureBox1.Width += zoomStep;
  22.     pictureBox1.Height += zoomStep;
  23.     //第②步
  24.     PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
  25.      BindingFlags.NonPublic);
  26.     Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
  27.     //第③步
  28.     pictureBox1.Width = rect.Width;
  29.     pictureBox1.Height = rect.Height;
  30.    }
  31.    if (e.Delta < 0) //缩小
  32.    {
  33.     //防止一直缩成负值
  34.     if (pictureBox1.Width < myBmp.Width / 10)
  35.      return;
  36.     
  37.     pictureBox1.Width -= zoomStep;
  38.     pictureBox1.Height -= zoomStep;
  39.     PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
  40.      BindingFlags.NonPublic);
  41.     Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
  42.     pictureBox1.Width = rect.Width;
  43.     pictureBox1.Height = rect.Height;
  44.    }
  45.    //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
  46.    VX = (int)((double)* (ow - pictureBox1.Width) / ow);
  47.    VY = (int)((double)* (oh - pictureBox1.Height) / oh);
  48.    pictureBox1.Location = new Point(pictureBox1.Location.+ VX, pictureBox1.Location.+ VY);
  49. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号