国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#控件picturebox實現(xiàn)圖像拖拽和縮放

2020-01-24 00:14:54
字體:
供稿:網(wǎng)友

本文實例為大家分享了C# picturebox實現(xiàn)圖像拖拽和縮放的具體代碼,供大家參考,具體內(nèi)容如下

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模式,根據(jù)圖像顯示大小更改picturebox大小,記錄鼠標位置補償縮放位移,實現(xiàn)錨點縮放,即以鼠標位置為中心進行縮放。
zoomstep --- 自己定義滾輪滑動縮放大小

代碼:

//實現(xiàn)錨點縮放(以鼠標所指位置為中心縮放);  //步驟:  //①先改picturebox長寬,長寬改變量一樣;  //②獲取縮放后picturebox中實際顯示圖像的長寬,這里長寬是不一樣的;  //③將picturebox的長寬設(shè)置為顯示圖像的長寬;  //④補償picturebox因縮放產(chǎn)生的位移,實現(xiàn)錨點縮放。  // 注釋:為啥要②③步?由于zoom模式的機制,把picturebox背景設(shè)為黑就知道為啥了。  //這里需要獲取zoom模式下picturebox所顯示圖像的大小信息,添加 using System.Reflection;  //pictureBox1_MouseWheel事件沒找到。。。手動添加,別忘在Form1.Designer.cs的“Windows 窗體設(shè)計器生成的代碼”里加入:    //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;  //因縮放產(chǎn)生的位移矢量   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;   }   //第④步,求因縮放產(chǎn)生的位移,進行補償,實現(xiàn)錨點縮放的效果   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);}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金华市| 巴林右旗| 沙洋县| 鄂尔多斯市| 菏泽市| 镇康县| 崇信县| 正宁县| 茌平县| 攀枝花市| 专栏| 北碚区| 庄河市| 当阳市| 揭阳市| 琼海市| 彰化市| 河津市| 芜湖县| 西丰县| 怀来县| 奎屯市| 永清县| 垦利县| 横山县| 大余县| 加查县| 三亚市| 黄平县| 壤塘县| 勃利县| 靖州| 甘孜县| 招远市| 台州市| 托克托县| 镇原县| 三江| 汾西县| 洪江市| 城口县|