C# PictureBox 放大、缩小、移动图片 画辅助线

C# PictureBox 放大、缩小、移动图片

 class PictureBoxEx
    {
        private static bool isMove = false;
        private static Point mouseDownPoint;
        private static int zoomStep = 30;

        public static int ZoomStep
        {
            get
            {
                return zoomStep;
            }

            set
            {
                zoomStep = value;
            }
        }

        /// <summary>
        /// 加载IMG
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="path"></param>
        public static void loadImg(object sender, string path)
        {
            PictureBox pbox = sender as PictureBox;


            pbox.MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown);
            pbox.MouseMove += new System.Windows.Forms.MouseEventHandler(MouseMove);
            pbox.MouseUp   += new System.Windows.Forms.MouseEventHandler(MouseUp);


            Image img = Image.FromFile(path);
            pbox.Image = img;

        }

        public static void MouseMove(object sender, MouseEventArgs e)
        {
            PictureBox pbox = sender as PictureBox;
            pbox.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 = pbox.Location.X + moveX;
                y = pbox.Location.Y + moveY;
                pbox.Location = new Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }

        }

        public static void MouseWheel(object sender, MouseEventArgs e)
        {
            PictureBox pbox = sender as PictureBox;
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pbox.Width;
            int oh = pbox.Height;
            int VX, VY;  //因缩放产生的位移矢量
            if (e.Delta > 0) //放大
            {
                //第①步
                pbox.Width += ZoomStep;
                pbox.Height += ZoomStep;
                //第②步
                PropertyInfo pInfo = pbox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pbox, null);
                //第③步
                pbox.Width = rect.Width;
                pbox.Height = rect.Height;
            }
            if (e.Delta < 0) //缩小
            {
                //防止一直缩成负值
                if (pbox.Width < 300)
                    return;

                pbox.Width -= ZoomStep;
                pbox.Height -= ZoomStep;
                PropertyInfo pInfo = pbox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                 BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pbox, null);
                pbox.Width = rect.Width;
                pbox.Height = rect.Height;
            }
            //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
            VX = (int)((double)x * (ow - pbox.Width) / ow);
            VY = (int)((double)y * (oh - pbox.Height) / oh);
            pbox.Location = new Point(pbox.Location.X + VX, pbox.Location.Y + VY);
        }


        public static void MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }
        }

        public static void MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox pbox = sender as PictureBox;
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
                pbox.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点
            }
        }

    }

画网格和辅助线

 private void drawGrids(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen myPen = Pens.Blue;
            for (int i = 0; i < ClientRectangle.Width; i++)
            {
                g.DrawLine(myPen, new Point(i, 0), new Point(i, ClientRectangle.Bottom));
                i += 50;
            }

            for (int j = 0; j < ClientRectangle.Height; j++)
            {
                g.DrawLine(myPen, new Point(0, j), new Point(ClientRectangle.Right, j));
                j += 50;
            }
        }

        private void drawLine(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen myPen = Pens.BlueViolet;

            int i = pictureBox1.Width / 2;
            int j = pictureBox1.Height / 2;

            g.DrawLine(myPen, new Point(i, 0), new Point(i, ClientRectangle.Bottom));

            g.DrawLine(myPen, new Point(0, j), new Point(ClientRectangle.Right, j));
        }
 private void pictureBox1_Paint (object sender, PaintEventArgs e) {
     if (checkBox1.Checked) {
         //drawGrids(e);
         drawLine (e);
     }
 }

 private void checkBox1_CheckStateChanged (object sender, EventArgs e) {
     pictureBox1.Refresh ();

 }

 

  • 14
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果需要在C#中实现PictureBox放大缩小并可移动,可以使用以下步骤: 1. 在PictureBox控件上添加MouseDown、MouseMove和MouseUp事件处理程序。 2. 在MouseDown事件处理程序中记录鼠标按下时的位置,以便在MouseMove事件中计算鼠标移动的距离。 3. 在MouseMove事件处理程序中,计算鼠标移动的距离并将PictureBox控件的Location属性相应地调整。 4. 在MouseUp事件处理程序中重置记录鼠标按下时位置的变量。 5. 在放大缩小按钮的Click事件中,同样修改PictureBox的Size属性。 以下是一个示例代码: ``` private bool isDragging = false; private Point lastCursor; private Point lastForm; private double zoom = 1.0; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { isDragging = true; lastCursor = Cursor.Position; lastForm = this.Location; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (isDragging) { int xDiff = Cursor.Position.X - lastCursor.X; int yDiff = Cursor.Position.Y - lastCursor.Y; this.Location = new Point(lastForm.X + xDiff, lastForm.Y + yDiff); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { isDragging = false; } private void btnZoomIn_Click(object sender, EventArgs e) { zoom *= 1.1; pictureBox1.Size = new Size((int)(pictureBox1.Width * zoom), (int)(pictureBox1.Height * zoom)); } private void btnZoomOut_Click(object sender, EventArgs e) { zoom *= 0.9; pictureBox1.Size = new Size((int)(pictureBox1.Width * zoom), (int)(pictureBox1.Height * zoom)); } ``` 在这个示例中,我们使用了isDragging变量来记录鼠标是否按下,并在PictureBox的MouseDown、MouseMove和MouseUp事件处理程序中进行相应的操作。同时,我们使用了lastCursor和lastForm变量来记录鼠标按下时的位置和窗体的位置,以便在MouseMove事件中计算鼠标移动的距离。 在放大缩小按钮的Click事件中,我们同样修改了PictureBox的Size属性,并使用了一个zoom变量来记录缩放倍数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值