c# 鼠标拖动缩放图片

通过鼠标操作改变pictureBox1的 大小和位置来调整里面图片的缩放和拖动

Bitmap myBmp;
        Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
        bool isMove = false;    //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
        int zoomStep = 10;//缩放比例


myBmp = new Bitmap(openFileDlg.FileName);
                            pictureBox1.BackgroundImage = myBmp;
                            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
                            pictureBox1.Location = new System.Drawing.Point((panel1.Width - pictureBox1.Width) / 2, (panel1.Height - pictureBox1.Height) / 2);
                            //pictureBox1.Image = myBmp;
                            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
                            pictureBox1.Width = myBmp.Width;
                            pictureBox1.Height = myBmp.Height;


#region =鼠标操作picturebox=
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
            if (pictureBox1.BackgroundImage != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mouseDownPoint.X = Cursor.Position.X;
                    mouseDownPoint.Y = Cursor.Position.Y;
                    isMove = true;
                    pictureBox1.Focus();
                    //panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 128, (panel1.Height / 2) - pictureBox1.Location.Y - 152);
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (pictureBox1.BackgroundImage != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    isMove = false;
                    Cursor = Cursors.Default;
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.BackgroundImage != null)
            {
                pictureBox1.Focus();
                if (isMove)
                {
                    int x, y;
                    int moveX, moveY;
                    moveX = Cursor.Position.X - mouseDownPoint.X;
                    moveY = Cursor.Position.Y - mouseDownPoint.Y;
                    x = pictureBox1.Location.X + moveX;
                    y = pictureBox1.Location.Y + moveY;
                    //防止图片移出panel2裁剪框
                    if (y > (panel1.Height - 304) / 2 + 20 || y < ((panel1.Height - 304) / 2 + 304 - pictureBox1.Height) - 20 || x > (panel1.Width - 256) / 2 + 40 || x < ((panel1.Width - 256) / 2 + 256 - pictureBox1.Width) - 20)
                    {
                        return;
                    }
                    pictureBox1.Location = new Point(x, y);
                    mouseDownPoint.X = Cursor.Position.X;
                    mouseDownPoint.Y = Cursor.Position.Y;
                    //panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 107, (panel1.Height / 2) - pictureBox1.Location.Y - 150);

                    PrintCutArea();
                }
            }
        }
        //鼠标滚轮放大缩小
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (pictureBox1.BackgroundImage != null)
            {
                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.Location.Y > (panel1.Height - 304) / 2 || pictureBox1.Location.Y < ((panel1.Height - 304) / 2 + 304 - pictureBox1.Height) || pictureBox1.Location.X > (panel1.Width - 256) / 2 || pictureBox1.Location.X < ((panel1.Width - 256) / 2 + 256 - pictureBox1.Width))
                    {
                        return;
                    }
                    if (pictureBox1.Width < 256 || pictureBox1.Height < 304)
                    {
                        return;
                    } 
                    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);
                //panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 128, (panel1.Height / 2) - pictureBox1.Location.Y - 150);
                PrintCutArea();
            }
        }
        #endregion


转载于:https://www.cnblogs.com/yanergui/p/5014278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值