• 技术文章 >后端开发 >C#.Net教程

    C#开发实例-订制屏幕截图工具(七)添加放大镜功能的代码示例

    黄舟黄舟2017-03-14 13:35:48原创1924

    由于截图时可能需要精确截取某一部分,所以需要放大镜的功能,这样截取的时候才更容易定位截图的位置。

    添加PictureBox,name属性设置为“pictureBox_zoom”;

    php入门到就业线上直播课:进入学习


    在“Form1_Load”事件处理函数中添加以下代码:

    //设置放大镜的大小
                this.pictureBox_zoom.Width = this.ZoomBoxWidth;
                this.pictureBox_zoom.Height = this.ZoomBoxHeight;

    在“ExitCutImage”方法中添加代码:

    在“Form1_MouseUp”事件处理函数中添加代码:


    在“ShowForm”方法的else条件最后添加代码:

    if (this.ZoomBoxVisible)
                    {
                        UpdateCutInfoLabel(UpdateUIMode.ShowZoomBox);
                        this.pictureBox_zoom.Show();
                    }

    在“UpdateCutInfoLabel”函数最后添加以下代码:

    if (this.pictureBox_zoom.Visible || (updateUIMode & UpdateUIMode.ShowZoomBox) != UpdateUIMode.None)
                {
                    Point zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y + 22);
                    if (zoomLocation.Y + this.pictureBox_zoom.Height > this.Height)
                    {
                        if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)
                        {
                            zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 10, MousePosition.Y - this.pictureBox_zoom.Height - 10);
                        }
                        else
                        {
                            zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y - this.pictureBox_zoom.Height - 15);
                        }
                    }
                    else
                    {
                        if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)
                        {
                            zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 15, MousePosition.Y);
                        }
                    }
                    this.pictureBox_zoom.Location = zoomLocation;
                    if (!this.pictureBox_zoom.Visible)
                    {
                        this.pictureBox_zoom.Show();
                    }
                }

    在“Form1_KeyUp”事件处理函数中添加以下代码:


    为“pictureBox_zoom”添加“Paint”事件处理程序,代码如下:

            /// <summary>
            /// 放大镜组件重绘事件处理程序
            /// 实时显示鼠标指针位置放大后的图像
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void pictureBox_zoom_Paint(object sender, PaintEventArgs e)
            {
                Bitmap bmp_lbl = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);
                int srcWidth = (int)(this.ZoomBoxWidth / 10);
                int srcHeight = (int)(this.ZoomBoxHeight / 10);
    
                Bitmap bmp = new Bitmap(srcWidth, srcHeight);
                Rectangle srcRect = new Rectangle(MousePosition.X - 5, MousePosition.Y - 4, srcWidth, srcHeight);
                if (!isCuting)
                {
                    srcRect = new Rectangle(MousePosition.X - 6, MousePosition.Y - 5, srcWidth, srcHeight);
                }
                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(screenImage, 0, 0, srcRect, GraphicsUnit.Pixel);
                g.Dispose();
    
                //Zoom
                int x, y;
                for (int row = 0; row < bmp.Height; row++)
                {
                    for (int col = 0; col < bmp.Width; col++)
                    {
                        Color pc = bmp.GetPixel(col, row);
                        for (int h = 0; h < 10; h++)
                        {
                            for (int w = 0; w < 10; w++)
                            {
                                x = col * 10 + w;
                                y = row * 10 + h;
                                if (x < bmp_lbl.Width && y < bmp_lbl.Height)
                                {
                                    bmp_lbl.SetPixel(x, y, pc);
                                }
                            }
                        }
                    }
                }
    
                e.Graphics.DrawImage(bmp_lbl, 0, 0);
    
                int blockX = e.ClipRectangle.Width / 2;
                int blockY = e.ClipRectangle.Height / 2;
    
                SolidBrush brush = new SolidBrush(Color.FromArgb(10, 124, 202));
                Pen pen = new Pen(brush, 2.0F);
                e.Graphics.DrawLine(pen, new Point(0, blockY), new Point(e.ClipRectangle.Width, blockY));
                e.Graphics.DrawLine(pen, new Point(blockX, 0), new Point(blockX, e.ClipRectangle.Height));
    
                g.Dispose();
                bmp_lbl.Dispose();
            }

    编译,运行,截图看看效果吧!

    以上就是C#开发实例-订制屏幕截图工具(七)添加放大镜功能的代码示例的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    上一篇:C#开发实例-订制屏幕截图工具(六)添加配置管理功能详解(图文) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• c语言中源文件编译后生成什么文件• c语言标识符有哪些类型• C#中GDI+编程10个基本技巧二• ASP.NET使用Ajax如何返回Json对象的方法具体介绍• 应用绝对路径与相对路径
    1/1

    PHP中文网