国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼

2019-10-29 21:09:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文是利用C# 開(kāi)發(fā)截圖軟件的小例子,以供學(xué)習(xí)分享使用。

思路:

  1. 截取屏幕圖片。
  2. 獲取要截取的范圍,即左上角,右下角坐標(biāo)
  3. 填充到PictureBox中。
  4. 筆觸功能,熒光筆,矩形,橡皮擦,復(fù)制,保存功能

涉及的知識(shí)點(diǎn):

  • MenuStrip:為窗體提供菜單系統(tǒng)。以ToolStripMenuItem為菜單子選項(xiàng)
  • ToolStrip:為 Windows 工具欄對(duì)象提供容器。以ToolStripButton【表示包含文本和圖像的可選】為工具欄子元素
  • PictureBox:表示用于顯示圖像的 Windows 圖片框控件。不過(guò)本文對(duì)此空間進(jìn)行了重寫(xiě)
  • Screen:可用于獲取工作屏幕區(qū)域
  • Graphics:封裝一個(gè) GDI+ 繪圖圖面。此類(lèi)不能被繼承。此類(lèi)的CopyFromScreen方法用于獲取屏幕圖像
  • 鼠標(biāo)事件:包括MouseDown,MouseMove,MouseUp事件,通過(guò)MouseEventArgs中的Location獲取鼠標(biāo)的位置。
  • Clipboard: 提供將數(shù)據(jù)置于系統(tǒng)剪貼板中以及從中檢索數(shù)據(jù)的方法。此類(lèi)不能被繼承。
  • Cursor:設(shè)置鼠標(biāo)的顯示的光標(biāo)的樣式。
  • OnPaint:重繪事件,當(dāng)控件刷新時(shí)響應(yīng)此事件。
  •  

效果圖如下【主要實(shí)現(xiàn)了截圖,保存,復(fù)制,畫(huà)矩形,筆觸,熒光筆,橡皮擦等功能】:

c#,截圖軟件

保存后圖片如下:

c#,截圖軟件

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

核心代碼如下:

截取屏幕圖像:

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()); //畫(huà)平滑曲線   } } /// <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); //畫(huà)平滑曲線   } } 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ì)VEVB武林網(wǎng)網(wǎng)站的支持!

 


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 改则县| 聂荣县| 腾冲县| 兖州市| 隆林| 巴里| 台东县| 城步| 略阳县| 安仁县| 青浦区| 平安县| 洪江市| 岗巴县| 巢湖市| 南木林县| 嘉义市| 金门县| 和平县| 拉萨市| 杭州市| 金山区| 开原市| 灵璧县| 乌审旗| 新乐市| 阿勒泰市| 阿克苏市| 安龙县| 贵阳市| 博乐市| 宾阳县| 海盐县| 龙川县| 高陵县| 台中市| 陇南市| 新河县| 康保县| 福海县| 南雄市|