winform中图片的旋转、缩放及拖动


        private Point _mouseDownPoint;
        private bool _allowMove;
        private int _zoomTimes;


        #region 图片左、右旋转
        private void btnTurnLeft_Click(object sender, EventArgs e)
        {
            if (picImage.Image == null) return;
            picImage.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
            picImage.Image = picImage.Image;
        }

        private void btnTurnRight_Click(object sender, EventArgs e)
        {
            if (picImage.Image == null) return;
            picImage.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
            picImage.Refresh();
        } 
        #endregion
         
        #region 滚轮事件
        private void picImage_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta >= 0)
            {
                if (_zoomTimes > 6)
                {
                    return;
                }
                _zoomTimes++;
            }
            else
            {
                if (_zoomTimes < -2)
                {
                    return;
                }
                _zoomTimes--;
            }

            ZoomImage();
        }

        /// <summary>
        /// 缩放图片
        /// </summary>
        private void ZoomImage()
        {
            var locationX = 0;
            var locationY = 0;
            picImage.Width = (int)(pnlContainer.Width * (1 + 0.25 * _zoomTimes));
            picImage.Height = (int)(pnlContainer.Height * (1 + 0.25 * _zoomTimes));

            //图片缩小时,显示在正中央
            if (picImage.Width < pnlContainer.Width)
            {
                locationX = (pnlContainer.Width - picImage.Width) / 2;
                locationY = (pnlContainer.Height - picImage.Height) / 2;
            }
            else if (picImage.Width > pnlContainer.Width)
            {
                locationX = picImage.Location.X;
                locationY = picImage.Location.Y;
                if (locationX < pnlContainer.Width - picImage.Width)
                {
                    locationX = pnlContainer.Width - picImage.Width;
                }
                if (locationX < pnlContainer.Height - picImage.Height)
                {
                    locationY = pnlContainer.Height - picImage.Height;
                }
            }
            else if (picImage.Width == pnlContainer.Width)
            {
                locationX = locationY = 0;
            }
            picImage.Location = new Point(locationX, locationY);
        }


        private void picImage_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _allowMove = true;
                _mouseDownPoint = new Point(e.X, e.Y);
            }
            picImage.Focus();
        }

        private void picImage_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _allowMove = false;
            }
        }
        
        #endregion

        #region 移动图片

        private void picImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (picImage.Width < pnlContainer.Width)
            {
                return;
            }
            if (_allowMove)
            {
                picImage.Location = new Point(GetLocation(true, e), GetLocation(false, e));
            }
        }

        /// <summary>
        /// 取得图片的定位
        /// </summary>
        /// <param name="isX">为true表示取得X轴的定位,否则是表示取得Y轴的定位</param>
        /// <param name="e"></param>
        /// <returns></returns>
        private int GetLocation(bool isX, MouseEventArgs e)
        {
            var x = picImage.Location.X - _mouseDownPoint.X + e.X;
            var y = picImage.Location.Y - _mouseDownPoint.Y + e.Y;
            if (isX)
            {
                if (x > 0)
                {
                    _mouseDownPoint.X = e.X;
                    return 0;
                }
                if (x < pnlContainer.Width - picImage.Width)
                {
                    _mouseDownPoint.X = e.X;
                    return pnlContainer.Width - picImage.Width;
                }
                return x;
            }

            if (y > 0)
            {
                _mouseDownPoint.Y = e.Y;
                return 0;
            }
            if (y < pnlContainer.Height - picImage.Height)
            {
                _mouseDownPoint.Y = e.Y;
                return pnlContainer.Height - picImage.Height;
            }
            return y;
        }

        #endregion

 

注:pnlContainer为picImage在父容器

   picImage.SizeMode= PictureBoxSizeMode.StretchImage;

  picImage.Cursor = Cursors.Hand;


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM