C#影象顯示實現拖拽、錨點縮放功能【轉】

Simmy.臥龍先生發表於2015-09-16

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大小,記錄滑鼠位置補償縮放位移,實現錨點縮放,即以滑鼠位置為中心進行縮放。
程式碼:
//實現錨點縮放(以滑鼠所指位置為中心縮放);
        //步驟:
        //①先改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);
        }

 

相關文章