欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#控件picturebox实现图像拖拽和缩放

程序员文章站 2023-12-16 13:40:46
本文实例为大家分享了c# picturebox实现图像拖拽和缩放的具体代码,供大家参考,具体内容如下 1.核心步骤: ①新建point类型全局变量mousedownpo...

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

1.核心步骤:

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

②mousedown事件记录cursor位置;

③mousemove事件计算移动矢量,并更新picturebox1.location。

代码:

private void picturebox1_mousedown(object sender, mouseeventargs e)
  {
   if (e.button == mousebuttons.left)
   {
    mousedownpoint.x = cursor.position.x; //记录鼠标左键按下时位置
    mousedownpoint.y = cursor.position.y;    
    ismove = true;
    picturebox1.focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点
   }
  }

  private void picturebox1_mouseup(object sender, mouseeventargs e)
  {
   if (e.button == mousebuttons.left)
   {
    ismove = false;    
   }
  }

  private void picturebox1_mousemove(object sender, mouseeventargs e)
  {
   picturebox1.focus(); //鼠标在picturebox上时才有焦点,此时可以缩放
   if (ismove)
   {
    int x, y;   //新的picturebox1.location(x,y)
    int movex, movey; //x方向,y方向移动大小。
    movex = cursor.position.x - mousedownpoint.x;
    movey = cursor.position.y - mousedownpoint.y;
    x = picturebox1.location.x + movex;
    y = picturebox1.location.y + movey;    
    picturebox1.location = new point(x, y);
    mousedownpoint.x = cursor.position.x;
    mousedownpoint.y = cursor.position.y;    
   }
  }
  
  private void panel2_mousedown(object sender, mouseeventargs e)
  {
   if (e.button == mousebuttons.left)
   {
    mousedownpoint.x = cursor.position.x; //记录鼠标左键按下时位置
    mousedownpoint.y = cursor.position.y;
    ismove = true;
   }
  }

  private void panel2_mouseup(object sender, mouseeventargs e)
  {
   if (e.button == mousebuttons.left)
   {
    ismove = false;
   }
  }

  private void panel2_mousemove(object sender, mouseeventargs e)
  {
   panel2.focus(); //鼠标不在picturebox上时焦点给别的控件,此时无法缩放   
   if (ismove)
   {
    int x, y;   //新的picturebox1.location(x,y)
    int movex, movey; //x方向,y方向移动大小。
    movex = cursor.position.x - mousedownpoint.x;
    movey = cursor.position.y - mousedownpoint.y;
    x = picturebox1.location.x + movex;
    y = picturebox1.location.y + movey;
    picturebox1.location = new point(x, y);
    mousedownpoint.x = cursor.position.x;
    mousedownpoint.y = cursor.position.y;
   }
  }

2.图像缩放

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

代码:

//实现锚点缩放(以鼠标所指位置为中心缩放);
  //步骤:
  //①先改picturebox长宽,长宽改变量一样;
  //②获取缩放后picturebox中实际显示图像的长宽,这里长宽是不一样的;
  //③将picturebox的长宽设置为显示图像的长宽;
  //④补偿picturebox因缩放产生的位移,实现锚点缩放。
  // 注释:为啥要②③步?由于zoom模式的机制,把picturebox背景设为黑就知道为啥了。
  //这里需要获取zoom模式下picturebox所显示图像的大小信息,添加 using system.reflection;
  //picturebox1_mousewheel事件没找到。。。手动添加,别忘在form1.designer.cs的“windows 窗体设计器生成的代码”里加入:  
  //this.picturebox1.mousewheel += new system.windows.forms.mouseeventhandler(this.picturebox1_mousewheel)。
  private void picturebox1_mousewheel(object sender, mouseeventargs e)
  {
   int x = e.location.x;
   int y = e.location.y;
   int ow = picturebox1.width;
   int oh = picturebox1.height;   
   int vx, vy;  //因缩放产生的位移矢量
   if (e.delta > 0) //放大
   {
    //第①步
    picturebox1.width += zoomstep;
    picturebox1.height += zoomstep;
    //第②步
    propertyinfo pinfo = picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance |
     bindingflags.nonpublic);
    rectangle rect = (rectangle)pinfo.getvalue(picturebox1, null);
    //第③步
    picturebox1.width = rect.width;
    picturebox1.height = rect.height;
   }
   if (e.delta < 0) //缩小
   {
    //防止一直缩成负值
    if (picturebox1.width < mybmp.width / 10)
     return;
    
    picturebox1.width -= zoomstep;
    picturebox1.height -= zoomstep;
    propertyinfo pinfo = picturebox1.gettype().getproperty("imagerectangle", bindingflags.instance |
     bindingflags.nonpublic);
    rectangle rect = (rectangle)pinfo.getvalue(picturebox1, null);
    picturebox1.width = rect.width;
    picturebox1.height = rect.height;
   }
   //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
   vx = (int)((double)x * (ow - picturebox1.width) / ow);
   vy = (int)((double)y * (oh - picturebox1.height) / oh);
   picturebox1.location = new point(picturebox1.location.x + vx, picturebox1.location.y + vy);
}

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

上一篇:

下一篇: