C # implement the screenshot software function,

Source: Internet
Author: User
Tags linecap

C # implement software functions,

This article is a small example of using C # To develop software for learning and sharing.

Ideas:

Knowledge points involved:

  • MenuStrip: provides a menu system for the form. Use ToolStripMenuItem as the menu sub-option
  • ToolStrip: Provides containers for Windows toolbar objects. Use ToolStripButton to [indicate the options that contain text and images] as a sub-element of the toolbar.
  • PictureBox: A Windows Image box control used to display images. However, this article overwrites this space.
  • Screen: used to obtain the work Screen area
  • Graphics: encapsulate a GDI + drawing. This class cannot be inherited. This CopyFromScreen method is used to obtain screen images.
  • Mouse events: including MouseDown, MouseMove, and MouseUp events. You can use the Location in MouseEventArgs to obtain the mouse position.
  • Clipboard: provides methods to place data in the system Clipboard and retrieve data from it. This class cannot be inherited.
  • Cursor: Set the Cursor style displayed by the mouse.
  • OnPaint: repaint event. The event is returned when the control is refreshed.

As shown in the following figure ]:

The saved image is as follows:

Bytes -------------------------------------------------------------------------------------------------------------------------------

The core code is as follows:

Screenshot screen image:

1 public Bitmap GetScreen () 2 {3 // obtain the entire Screen image, excluding taskbar 4 Rectangle ScreenArea = Screen. getWorkingArea (this); 5 Bitmap bmp = new Bitmap (ScreenArea. width, ScreenArea. height); 6 using (Graphics g = Graphics. fromImage (bmp) 7 {8g. copyFromScreen (0, 0, 0, 0, new Size (ScreenArea. width, ScreenArea. height); 9} 10 return bmp; 11}
View Code

Graphic drawing function:

1 # region painting function 2 3 protected override void OnPaint (PaintEventArgs pe) 4 {5 base. onPaint (pe); 6 Graphics g = pe. graphics; 7 DrawHistory (g); 8 // draw the current line 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 // draw line history 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 // draw a rectangle history 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 // draw line 45 /// </summary> 46 // <param name = "g "> </param> 47 // <param name =" line "> </param> 48 private void DrawLine (Graphics g, HLine) {49g. smoothingMode = SmoothingMode. antiAlias; 50 using (Pen p = new Pen (line. lineColor, line. lineWidth) 51 {52 // set the start and end point of the line Cap 53 p. startCap = LineCap. round; 54 p. endCap = LineCap. round; 55 56 // set the join style for two consecutive segments 57 p. lineJoin = LineJoin. round; 58g. drawCurve (p, line. pointList. toArray ()); // draw a smooth curve 59} 60} 61 62 // <summary> 63 // draw a rectangle 64 // </summary> 65 // <param name =" g "> </param> 66 // <param name =" rect "> </param> 67 private void DrawRectangle (Graphics g, HRectangle rect) 68 {69g. smoothingMode = SmoothingMode. antiAlias; 70 using (Pen p = new Pen (rect. lineColor, rect. lineWidth) 71 {72 // set the start and end point line Cap 73 p. startCap = LineCap. round; 74 p. endCap = LineCap. round; 75 76 // set the join style for two consecutive segments 77 p. lineJoin = LineJoin. round; 78g. drawRectangle (p, rect. start. x, rect. start. y, rect. end. x-rect. start. x, rect. end. y-rect. start. y); // draw a smoothing curve 79} 80} 81 82 public void Earser (Point p0) 83 {84 for (int I = lineHistory. count-1; I> = 0; I --) 85 {86 HLine = 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 // Delete 94 flag = true; 95 break; 96} 97 98} 99 if (flag) 100 {101 lineHistory. removeAt (I); 102} 103} 104 // erase the rectangle 105 for (int I = rectHistory. count-1; I> = 0; I --) 106 {107 HRectangle rect = rectHistory [I]; 108 109 if (distinct x> rect. start. X & amp; # X <rect. end. X & Policy> rect. start. Y & Policy <rect. end. y) {110 111 rectHistory. removeAt (I ); 112} 113} 114} 115 116 // <summary> 117 // obtain the distance between two points 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 # endregion
View Code


The following is a connection to the source code function. If you need it, you can download it by yourself.

Source code Link

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.