C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
本文是利用C# 開發(fā)截圖軟件的小例子,以供學(xué)習(xí)分享使用。
思路:
- 截取屏幕圖片。
 - 獲取要截取的范圍,即左上角,右下角坐標(biāo)
 - 填充到PictureBox中。
 - 筆觸功能,熒光筆,矩形,橡皮擦,復(fù)制,保存功能
 
涉及的知識(shí)點(diǎn):
- MenuStrip:為窗體提供菜單系統(tǒng)。以ToolStripMenuItem為菜單子選項(xiàng)
 - ToolStrip:為 Windows 工具欄對(duì)象提供容器。以ToolStripButton【表示包含文本和圖像的可選】為工具欄子元素
 - PictureBox:表示用于顯示圖像的 Windows 圖片框控件。不過(guò)本文對(duì)此空間進(jìn)行了重寫
 - Screen:可用于獲取工作屏幕區(qū)域
 - Graphics:封裝一個(gè) GDI+ 繪圖圖面。此類不能被繼承。此類的CopyFromScreen方法用于獲取屏幕圖像
 - 鼠標(biāo)事件:包括MouseDown,MouseMove,MouseUp事件,通過(guò)MouseEventArgs中的Location獲取鼠標(biāo)的位置。
 - Clipboard: 提供將數(shù)據(jù)置于系統(tǒng)剪貼板中以及從中檢索數(shù)據(jù)的方法。此類不能被繼承。
 - Cursor:設(shè)置鼠標(biāo)的顯示的光標(biāo)的樣式。
 - OnPaint:重繪事件,當(dāng)控件刷新時(shí)響應(yīng)此事件。
 
效果圖如下【主要實(shí)現(xiàn)了截圖,保存,復(fù)制,畫矩形,筆觸,熒光筆,橡皮擦等功能】:
保存后圖片如下:
-------------------------------------------------------------------------------------------------------------------------------
核心代碼如下:
截取屏幕圖像:
public Bitmap GetScreen()
 {
  //獲取整個(gè)屏幕圖像,不包括任務(wù)欄
  Rectangle ScreenArea = Screen.GetWorkingArea(this);
  Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
  using (Graphics g = Graphics.FromImage(bmp))
  {
  g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenArea.Width,ScreenArea.Height));
  }
  return bmp;
 }
繪制圖形功能:
#region 繪制功能
 protected override void OnPaint(PaintEventArgs pe)
 {
  base.OnPaint(pe);
  Graphics g = pe.Graphics;
  DrawHistory(g);
  //繪制當(dāng)前線
  if (startDraw && this.curLine.PointList != null && this.curLine.PointList.Count > 0)
  {
  DrawLine(g,this.curLine);
  }
  if (startDraw && this.curRect.Start != null && this.curRect.End != null && this.curRect.Start != this.curRect.End) {
  DrawRectangle(g, this.curRect);
  }
 }
 public void DrawHistory(Graphics g) {
  //繪制線歷史記錄
  if (LineHistory != null)
  {
  foreach (HLine lh in LineHistory)
  {
   if (lh.PointList.Count > 10)
   {
   DrawLine(g, lh);
   }
  }
  }
  //繪制矩形歷史記錄
  if (RectHistory != null)
  {
  foreach (HRectangle lh in RectHistory)
  {
   if (lh.Start!=null&& lh.End!=null && lh.Start!=lh.End)
   {
   DrawRectangle(g, lh);
   }
  }
  }
 }
 /// <summary>
 /// 繪制線
 /// </summary>
 /// <param name="g"></param>
 /// <param name="line"></param>
 private void DrawLine(Graphics g,HLine line) {
  g.SmoothingMode = SmoothingMode.AntiAlias;
  using (Pen p = new Pen(line.LineColor, line.LineWidth))
  {
  //設(shè)置起止點(diǎn)線帽 
  p.StartCap = LineCap.Round;
  p.EndCap = LineCap.Round;
  //設(shè)置連續(xù)兩段的聯(lián)接樣式 
  p.LineJoin = LineJoin.Round;
  g.DrawCurve(p, line.PointList.ToArray()); //畫平滑曲線 
  }
 }
 /// <summary>
 /// 繪制矩形
 /// </summary>
 /// <param name="g"></param>
 /// <param name="rect"></param>
 private void DrawRectangle(Graphics g, HRectangle rect)
 {
  g.SmoothingMode = SmoothingMode.AntiAlias;
  using (Pen p = new Pen(rect.LineColor, rect.LineWidth))
  {
  //設(shè)置起止點(diǎn)線帽 
  p.StartCap = LineCap.Round;
  p.EndCap = LineCap.Round;
  //設(shè)置連續(xù)兩段的聯(lián)接樣式 
  p.LineJoin = LineJoin.Round;
  g.DrawRectangle(p, rect.Start.X, rect.Start.Y, rect.End.X - rect.Start.X, rect.End.Y - rect.Start.Y); //畫平滑曲線 
  }
 }
 public void Earser(Point p0)
 {
  for (int i = lineHistory.Count - 1; i >= 0; i--)
  {
  HLine line = lineHistory[i];
  bool flag = false;
  foreach (Point p1 in line.PointList)
  {
   double distance = GetDistance(p0, p1);
   if (Math.Abs(distance) < 6)
   {
   //需要?jiǎng)h除
   flag = true;
   break;
   }
  }
  if (flag)
  {
   lineHistory.RemoveAt(i);
  }
  }
  //擦除矩形
  for (int i = rectHistory.Count - 1; i >= 0; i--)
  {
  HRectangle rect = rectHistory[i];
  
  if (p0.X>rect.Start.X && p0.X<rect.End.X && p0.Y > rect.Start.Y && p0.Y < rect.End.Y) {
   
   rectHistory.RemoveAt(i);
  }
  }
 }
 /// <summary>
 /// 獲取兩點(diǎn)之間的距離
 /// </summary>
 /// <param name="p0"></param>
 /// <param name="p1"></param>
 /// <returns></returns>
 private double GetDistance(Point p0, Point p1) {
  return Math.Sqrt(Math.Pow((p0.X - p1.X), 2) + Math.Pow((p0.Y - p1.Y), 2));
 }
 #endregion
以下是源碼功能連接,需要的朋友可以自行下載。
源碼鏈接
以上所述是小編給大家介紹的C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
欄 目:C#教程
本文標(biāo)題:C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5680.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 - 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
 - 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
 - 01-10C#自定義簽名章實(shí)現(xiàn)方法
 - 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
 - 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10delphi制作wav文件的方法
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 


