废话不多说,直接上图看效果,左上角是原图片大小,右边是局部放大的效果

主要代码贴在下面,picBox是原图控件名,picBox_Show是放大控件名

   private void picBox_Paint(object sender, PaintEventArgs e)
        {
            if (isMove == true)
            {
                Graphics g = e.Graphics;
                /*画长方形*/
                int _x = movedPoint_X - rect_W / 2;
                int _y = movedPoint_Y - rect_H / 2;
                _x = _x < 0 ? 0 : _x;
                _y = _y < 0 ? 0 : _y;
                _x = _x >= picBox.Width - rect_W ? picBox.Width - rect_W - 3 : _x; //减3px的目的就是为了让长方形的边框不会刚好被picBox的边框挡住了
                _y = _y >= picBox.Height - rect_H ? picBox.Height - rect_H - 3 : _y;
                Rectangle rect = new Rectangle(_x, _y, rect_W, rect_H);
                g.DrawRectangle(pen, rect);
                // g.FillRectangle(brush, rect);

                ///*填充网格*/
                int x1, x2, y1, y2;
                x1 = x2 = _x;
                y1 = y2 = _y;
                x2 += rect_W;
                for (int i = 1; i < rowGridCount; i++)
                {
                    y1 += gridSize;
                    y2 += gridSize;
                    g.DrawLine(pen, x1, y1, x2, y2);
                }
                x1 = x2 = _x;
                y1 = y2 = _y;
                y2 += rect_H;
                for (int j = 1; j < columnGridCount; j++)
                {
                    x1 += gridSize;
                    x2 += gridSize;
                    g.DrawLine(pen, x1, y1, x2, y2);
                }

                /*裁剪图片*/
                if (picBox_Show.Image != null)
                {
                    picBox_Show.Image.Dispose();
                }
                Bitmap bmp = (Bitmap)picBox.Image;
                //缩放比例
                double rate_W = Convert.ToDouble(bmp.Width) / picBox.Width;
                double rate_H = Convert.ToDouble(bmp.Height) / picBox.Height;

                Bitmap bmp2 = bmp.Clone(new Rectangle(Convert.ToInt32(rate_W * _x), Convert.ToInt32(rate_H * _y), Convert.ToInt32(rate_W * rect_W), Convert.ToInt32(rate_H * rect_H)), picBox.Image.PixelFormat);
                picBox_Show.Image = bmp2;
                picBox_Show.SizeMode = PictureBoxSizeMode.Zoom;
                picBox_Show.Visible = true;

                isMove = false;
            }
        }
        //Paint事件中处理逻辑:当鼠标移动在图片的某个位置时,我们需要绘个长方形区域,同时显示局部放大图片(picBox_Show)。当执行picBox.Refresh()方法时将触发该事件。
        private void picBox_MouseMove(object sender, MouseEventArgs e)
        {
            picBox.Focus(); //否则滚轮事件无效
            isMove = true;
            movedPoint_X = e.X;
            movedPoint_Y = e.Y;
            picBox.Refresh();
        }
        //鼠标移开后,局部显示图片(picBox_Show)隐藏,picBox绘制的长方形也要去掉,最简单的就是调用Refresh()方法了。
        void picBox_Show_MouseWheel(object sender, MouseEventArgs e)
        {
            double scale = 1;
            if (picBox_Show.Height > 0)
            {
                scale = (double)picBox_Show.Width / (double)picBox_Show.Height;
            }
            picBox_Show.Width += (int)(e.Delta * scale);
            picBox_Show.Height += e.Delta;
        }

        bool isMove = false;
        //鼠标移动后点的坐标
        int movedPoint_X, movedPoint_Y;
        //画笔颜色
        Pen pen = new Pen(Color.FromArgb(91, 98, 114));
        HatchBrush brush = new HatchBrush(HatchStyle.Cross, Color.FromArgb(91, 98, 114), Color.Empty); //使用阴影画笔画网格


        //选取区域的大小
        const int rect_W = 80;
        const int rect_H = 60;
        //网格边长:5px 一格
        const int gridSize = 2;
        //网格的行、列数
        int rowGridCount = rect_H / gridSize;
        int columnGridCount = rect_W / gridSize;
        private void picBox_MouseLeave(object sender, EventArgs e)
        {
            picBox_Show.Visible = false;
            picBox.Refresh();

            picBox_Show.Width = 400;
            picBox_Show.Height = 300;
        }

 

posted on 2018-07-31 11:19  wlmcc  阅读(590)  评论(1编辑  收藏  举报