本文是利用C# 開發截圖軟件的小例子,以供學習分享使用。
思路:
涉及的知識點:
效果圖如下【主要實現了截圖,保存,復制,畫矩形,筆觸,熒光筆,橡皮擦等功能】:
保存后圖片如下:
-------------------------------------------------------------------------------------------------------------------------------
核心代碼如下:
截取屏幕圖像:
public Bitmap GetScreen() { //獲取整個屏幕圖像,不包括任務欄 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); //繪制當前線 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)) { //設置起止點線帽 p.StartCap = LineCap.Round; p.EndCap = LineCap.Round; //設置連續兩段的聯接樣式 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)) { //設置起止點線帽 p.StartCap = LineCap.Round; p.EndCap = LineCap.Round; //設置連續兩段的聯接樣式 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) { //需要刪除 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> /// 獲取兩點之間的距離 /// </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# 實現截圖軟件功能實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答