Home >Backend Development >C#.Net Tutorial >C# operation example to implement screenshot function
This article is a small example of using C# to develop screenshot software for learning and sharing.
Ideas:
Capture screen pictures.
Get the range to be intercepted, that is, the coordinates of the upper left corner and lower right corner
and fill them into the PictureBox.
Stroke function, highlighter, rectangle, eraser, copy, save function
Involved knowledge points:
MenuStrip: Provides a menu system for the form. Take ToolStripMenuItem as menu sub-option
ToolStrip: Provides a container for Windows toolbar objects. Take ToolStripButton [representing an optional option containing text and images] as the toolbar sub-element
PictureBox: represents the Windows picture box control used to display images. However, this article rewrites this space
Screen: Can be used to obtain the working screen area
Graphics: Encapsulates a GDI+ drawing surface. This class cannot be inherited. The CopyFromScreen method of this class is used to obtain the screen image
Mouse events: including MouseDown, MouseMove, and MouseUp events, and obtain the position of the mouse through the Location in MouseEventArgs.
Clipboard: Provides methods for placing and retrieving data from the system clipboard. This class cannot be inherited.
Cursor: Set the style of the cursor displayed by the mouse.
OnPaint: Redraw event, respond to this event when the control refreshes.
The effect picture is as follows [mainly realizes the functions of screenshots, saving, copying, drawing rectangles, strokes, highlighters, erasers, etc.]:
The picture after saving is as follows:
-------------------------- -------------------------------------------------- -------------------------------------------------- -
The core code is as follows:
Capture screen image:
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 #endregionView Code
The above is the detailed content of C# operation example to implement screenshot function. For more information, please follow other related articles on the PHP Chinese website!