C# 實現截圖軟件功能

本文是利用C# 開發截圖軟件的小例子,以供學習分享使用。ide

思路:工具

  1. 截取屏幕圖片。
  2. 獲取要截取的範圍,即左上角,右下角座標
  3. 填充到PictureBox中。
  4. 筆觸功能,熒光筆,矩形,橡皮擦,複製,保存功能

涉及的知識點:學習

  • MenuStrip:爲窗體提供菜單系統。以ToolStripMenuItem爲菜單子選項
  • ToolStrip:爲 Windows 工具欄對象提供容器。以ToolStripButton【表示包含文本和圖像的可選】爲工具欄子元素
  • PictureBox:表示用於顯示圖像的 Windows 圖片框控件。不過本文對此空間進行了重寫
  • Screen:可用於獲取工做屏幕區域
  • Graphics:封裝一個 GDI+ 繪圖圖面。此類不能被繼承。此類的CopyFromScreen方法用於獲取屏幕圖像
  • 鼠標事件:包括MouseDown,MouseMove,MouseUp事件,經過MouseEventArgs中的Location獲取鼠標的位置。
  • Clipboard: 提供將數據置於系統剪貼板中以及從中檢索數據的方法。此類不能被繼承。
  • Cursor:設置鼠標的顯示的光標的樣式。
  • OnPaint:重繪事件,當控件刷新時響應此事件。

效果圖this

以下【主要實現了截圖,保存,複製,畫矩形,筆觸,熒光筆,橡皮擦等功能】:spa

保存後圖片以下:3d

-------------------------------------------------------------------------------------------------------------------------------code

核心代碼以下:對象

截取屏幕圖像:blog

 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, 0, 0, 0, new 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,this.curLine);
 12             }
 13             if (startDraw && this.curRect.Start != null && this.curRect.End != null && this.curRect.Start != this.curRect.End) {
 14                 DrawRectangle(g, this.curRect);
 15             }
 16         }
 17 
 18         public void 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 != null)
 32             {
 33                 foreach (HRectangle lh in RectHistory)
 34                 {
 35                     if (lh.Start!=null&& lh.End!=null && lh.Start!=lh.End)
 36                     {
 37                         DrawRectangle(g, lh);
 38                     }
 39                 }
 40             }
 41         }
 42 
 43         /// <summary>
 44         /// 繪製線
 45         /// </summary>
 46         /// <param name="g"></param>
 47         /// <param name="line"></param>
 48         private void DrawLine(Graphics g,HLine line) {
 49             g.SmoothingMode = SmoothingMode.AntiAlias;
 50             using (Pen p = new Pen(line.LineColor, line.LineWidth))
 51             {
 52                 //設置起止點線帽  
 53                 p.StartCap = LineCap.Round;
 54                 p.EndCap = LineCap.Round;
 55 
 56                 //設置連續兩段的聯接樣式  
 57                 p.LineJoin = LineJoin.Round;
 58                 g.DrawCurve(p, line.PointList.ToArray()); //畫平滑曲線  
 59             }
 60         }
 61 
 62         /// <summary>
 63         /// 繪製矩形
 64         /// </summary>
 65         /// <param name="g"></param>
 66         /// <param name="rect"></param>
 67         private void DrawRectangle(Graphics g, HRectangle rect)
 68         {
 69             g.SmoothingMode = SmoothingMode.AntiAlias;
 70             using (Pen p = new Pen(rect.LineColor, rect.LineWidth))
 71             {
 72                 //設置起止點線帽  
 73                 p.StartCap = LineCap.Round;
 74                 p.EndCap = LineCap.Round;
 75 
 76                 //設置連續兩段的聯接樣式  
 77                 p.LineJoin = LineJoin.Round;
 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         public void 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 in 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             for (int i = rectHistory.Count - 1; i >= 0; i--)
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         /// <summary>
117         /// 獲取兩點之間的距離
118         /// </summary>
119         /// <param name="p0"></param>
120         /// <param name="p1"></param>
121         /// <returns></returns>
122         private double GetDistance(Point p0, Point p1) {
123             return Math.Sqrt(Math.Pow((p0.X - p1.X), 2) + Math.Pow((p0.Y - p1.Y), 2));
124         }
125 
126         #endregion
View Code


如下是源碼功能鏈接,須要的朋友能夠自行下載。

源碼連接

相關文章
相關標籤/搜索