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

本文介绍了一种使用C#的PictureBox控件实现图片加载、放大、缩小和移动的方法。通过自定义PictureBoxEx类,实现了鼠标滚轮控制图片缩放、鼠标拖动移动图片的功能,并详细解释了代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ();

 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值