zoukankan      html  css  js  c++  java
  • C#软件开发实例.私人订制自己的屏幕截图工具(七)添加放大镜的功能

    本实例全部文章目录

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

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


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

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. //设置放大镜的大小  
    2.             this.pictureBox_zoom.Width = this.ZoomBoxWidth;  
    3.             this.pictureBox_zoom.Height = this.ZoomBoxHeight;  

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

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


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

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. if (this.ZoomBoxVisible)  
    2.                 {  
    3.                     UpdateCutInfoLabel(UpdateUIMode.ShowZoomBox);  
    4.                     this.pictureBox_zoom.Show();  
    5.                 }  

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

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. if (this.pictureBox_zoom.Visible || (updateUIMode & UpdateUIMode.ShowZoomBox) != UpdateUIMode.None)  
    2.             {  
    3.                 Point zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y + 22);  
    4.                 if (zoomLocation.Y + this.pictureBox_zoom.Height > this.Height)  
    5.                 {  
    6.                     if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)  
    7.                     {  
    8.                         zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 10, MousePosition.Y - this.pictureBox_zoom.Height - 10);  
    9.                     }  
    10.                     else  
    11.                     {  
    12.                         zoomLocation = new Point(MousePosition.X + 15, MousePosition.Y - this.pictureBox_zoom.Height - 15);  
    13.                     }  
    14.                 }  
    15.                 else  
    16.                 {  
    17.                     if (zoomLocation.X + this.pictureBox_zoom.Width > this.Width)  
    18.                     {  
    19.                         zoomLocation = new Point(MousePosition.X - this.pictureBox_zoom.Width - 15, MousePosition.Y);  
    20.                     }  
    21.                 }  
    22.                 this.pictureBox_zoom.Location = zoomLocation;  
    23.                 if (!this.pictureBox_zoom.Visible)  
    24.                 {  
    25.                     this.pictureBox_zoom.Show();  
    26.                 }  
    27.             }  

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


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

    [csharp] view plaincopy在CODE上查看代码片派生到我的代码片
    1. /// <summary>  
    2. /// 放大镜组件重绘事件处理程序  
    3. /// 实时显示鼠标指针位置放大后的图像  
    4. /// </summary>  
    5. /// <param name="sender"></param>  
    6. /// <param name="e"></param>  
    7. private void pictureBox_zoom_Paint(object sender, PaintEventArgs e)  
    8. {  
    9.     Bitmap bmp_lbl = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);  
    10.     int srcWidth = (int)(this.ZoomBoxWidth / 10);  
    11.     int srcHeight = (int)(this.ZoomBoxHeight / 10);  
    12.   
    13.     Bitmap bmp = new Bitmap(srcWidth, srcHeight);  
    14.     Rectangle srcRect = new Rectangle(MousePosition.X - 5, MousePosition.Y - 4, srcWidth, srcHeight);  
    15.     if (!isCuting)  
    16.     {  
    17.         srcRect = new Rectangle(MousePosition.X - 6, MousePosition.Y - 5, srcWidth, srcHeight);  
    18.     }  
    19.     Graphics g = Graphics.FromImage(bmp);  
    20.     g.DrawImage(screenImage, 0, 0, srcRect, GraphicsUnit.Pixel);  
    21.     g.Dispose();  
    22.   
    23.     //Zoom  
    24.     int x, y;  
    25.     for (int row = 0; row < bmp.Height; row++)  
    26.     {  
    27.         for (int col = 0; col < bmp.Width; col++)  
    28.         {  
    29.             Color pc = bmp.GetPixel(col, row);  
    30.             for (int h = 0; h < 10; h++)  
    31.             {  
    32.                 for (int w = 0; w < 10; w++)  
    33.                 {  
    34.                     x = col * 10 + w;  
    35.                     y = row * 10 + h;  
    36.                     if (x < bmp_lbl.Width && y < bmp_lbl.Height)  
    37.                     {  
    38.                         bmp_lbl.SetPixel(x, y, pc);  
    39.                     }  
    40.                 }  
    41.             }  
    42.         }  
    43.     }  
    44.   
    45.     e.Graphics.DrawImage(bmp_lbl, 0, 0);  
    46.   
    47.     int blockX = e.ClipRectangle.Width / 2;  
    48.     int blockY = e.ClipRectangle.Height / 2;  
    49.   
    50.     SolidBrush brush = new SolidBrush(Color.FromArgb(10, 124, 202));  
    51.     Pen pen = new Pen(brush, 2.0F);  
    52.     e.Graphics.DrawLine(pen, new Point(0, blockY), new Point(e.ClipRectangle.Width, blockY));  
    53.     e.Graphics.DrawLine(pen, new Point(blockX, 0), new Point(blockX, e.ClipRectangle.Height));  
    54.   
    55.     g.Dispose();  
    56.     bmp_lbl.Dispose();  
    57. }  

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

  • 相关阅读:
    Linux下OpenSSL自签ssl证书
    戌蛤怒触铁铁树
    Python自动化办公-自动化操作Excell-openpyxl
    Python正则表达式
    Django中F查询
    如何知道自家的宽带是多少兆?
    ‘Diango中明明模版的名称写的是对的,但是访问url的时候发现没调用模版’解决方案
    Django中的聚合函数
    Django中的跨关系查询
    【RabbitMQ】零、Windows64位安装
  • 原文地址:https://www.cnblogs.com/AlexanderZhao/p/12878972.html
Copyright © 2011-2022 走看看