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

本实例全部文章目录

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

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


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

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

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

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


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

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

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

[csharp]  view plain copy 在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 plain copy 在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. }  

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值