本文是利用C# 開(kāi)發(fā)截圖軟件的小例子,以供學(xué)習(xí)分享使用。
思路:
涉及的知識(shí)點(diǎn):
效果圖如下【主要實(shí)現(xiàn)了截圖,保存,復(fù)制,畫(huà)矩形,筆觸,熒光筆,橡皮擦等功能】:

保存后圖片如下:

-------------------------------------------------------------------------------------------------------------------------------
核心代碼如下:
截取屏幕圖像:
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)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注