加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 实现截图软件功能

发布时间:2020-12-16 01:14:10 所属栏目:百科 来源:网络整理
导读:本文是利用C# 开发截图软件的小例子,以供学习分享使用。 思路: 截取屏幕图片。 获取要截取的范围,即左上角,右下角坐标 填充到PictureBox中。 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能 涉及的知识点: MenuStrip:为窗体提供菜单系统。以ToolStrip

本文是利用C# 开发截图软件的小例子,以供学习分享使用。

思路:

  1. 截取屏幕图片。
  2. 获取要截取的范围,即左上角,右下角坐标
  3. 填充到PictureBox中。
  4. 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能

涉及的知识点:

  • MenuStrip:为窗体提供菜单系统。以ToolStripMenuItem为菜单子选项
  • ToolStrip:为 Windows 工具栏对象提供容器。以ToolStripButton【表示包含文本和图像的可选】为工具栏子元素
  • PictureBox:表示用于显示图像的 Windows 图片框控件。不过本文对此空间进行了重写
  • Screen:可用于获取工作屏幕区域
  • Graphics:封装一个 GDI+ 绘图图面。此类不能被继承。此类的CopyFromScreen方法用于获取屏幕图像
  • 鼠标事件:包括MouseDown,MouseMove,MouseUp事件,通过MouseEventArgs中的Location获取鼠标的位置。
  • Clipboard:?提供将数据置于系统剪贴板中以及从中检索数据的方法。此类不能被继承。
  • Cursor:设置鼠标的显示的光标的样式。
  • OnPaint:重绘事件,当控件刷新时响应此事件。

效果图

如下【主要实现了截图,保存,复制,画矩形,笔触,荧光笔,橡皮擦等功能】:

保存后图片如下:

-------------------------------------------------------------------------------------------------------------------------------

核心代码如下:

截取屏幕图像:

 1 public Bitmap GetScreen()
 2         {
 3             //获取整个屏幕图像,不包括任务栏
 4             Rectangle ScreenArea = Screen.GetWorkingArea(this);
 5             Bitmap bmp = new Bitmap(ScreenArea.Width,ScreenArea.Height);
 6             using (Graphics g = Graphics.FromImage(bmp))
 7             {
 8                 g.CopyFromScreen(0, Size(ScreenArea.Width,ScreenArea.Height));
 9             }
10             return bmp;
11         }
View Code

绘制图形功能:

  1 #region 绘制功能
  2 
  3         protected override void OnPaint(PaintEventArgs pe)
  4   5             base.OnPaint(pe);
  6             Graphics g = pe.Graphics;
  7             DrawHistory(g);
  8             绘制当前线
  9             if (startDraw && this.curLine.PointList != null && this.curLine.PointList.Count > 0)
 10  11                 DrawLine(g,1)">.curLine);
 12  13             this.curRect.Start != this.curRect.End != .curRect.End) {
 14                 DrawRectangle(g,1)">.curRect);
 15  16         }
 17 
 18         public  DrawHistory(Graphics g) {
 19             绘制线历史记录
 20             if (LineHistory != null 21  22                 foreach (HLine lh in LineHistory)
 23                 {
 24                     if (lh.PointList.Count > 10 25                     {
 26                         DrawLine(g,lh);
 27                     }
 28                 }
 29  30             绘制矩形历史记录
 31             if (RectHistory !=  32  33                 foreach (HRectangle lh  RectHistory)
 34  35                     if (lh.Start!=null&& lh.End!=null && lh.Start!=lh.End)
 36  37                         DrawRectangle(g,1)"> 38  39  40  41  42 
 43         /// <summary>
 44         /// 绘制线
 45         </summary>
 46         <param name="g"></param>
 47         <param name="line"></param>
 48         private  DrawLine(Graphics g,HLine line) {
 49             g.SmoothingMode = SmoothingMode.AntiAlias;
 50             using (Pen p =  Pen(line.LineColor,line.LineWidth))
 51  52                 设置起止点线帽  
 53                 p.StartCap = LineCap.Round;
 54                 p.EndCap = 55 
 56                 设置连续两段的联接样式  
 57                 p.LineJoin = LineJoin.Round;
 58                 g.DrawCurve(p,line.PointList.ToArray()); 画平滑曲线  
 59  60  61 
 62          63          绘制矩形
 64          65          66         <param name="rect"></param>
 67          DrawRectangle(Graphics g,HRectangle rect)
 68  69             g.SmoothingMode = 70              Pen(rect.LineColor,rect.LineWidth))
 71  72                  73                 p.StartCap = 74                 p.EndCap = 75 
 76                  77                 p.LineJoin = 78                 g.DrawRectangle(p,rect.Start.X,rect.Start.Y,rect.End.X - rect.Start.X,rect.End.Y - rect.Start.Y);  79  80  81 
 82          Earser(Point p0)
 83  84             for (int i = lineHistory.Count - 1; i >= 0; i-- 85  86                 HLine line = lineHistory[i];
 87                 bool flag = false;
 88                 foreach (Point p1  line.PointList)
 89  90                     double distance = GetDistance(p0,p1);
 91                     if (Math.Abs(distance) < 6 92  93                         需要删除
 94                         flag = true 95                         break 96  97 
 98  99                 if (flag)
100 101                     lineHistory.RemoveAt(i);
102 103 104             擦除矩形
105             int i = rectHistory.Count - 106 107                 HRectangle rect = rectHistory[i];
108                
109                 if (p0.X>rect.Start.X && p0.X<rect.End.X && p0.Y > rect.Start.Y && p0.Y < rect.End.Y) {
110                    
111                     rectHistory.RemoveAt(i);
112 113 114 115  
116         117          获取两点之间的距离
118         119         <param name="p0"></param>
120         <param name="p1"></param>
121         <returns></returns>
122         double GetDistance(Point p0,Point p1) {
123             return Math.Sqrt(Math.Pow((p0.X - p1.X),1)">2) + Math.Pow((p0.Y - p1.Y),1)">2));
124 125 
126         #endregion
View Code


以下是源码功能连接,需要的朋友可以自行下载。

源码链接

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读