WinForm Screen Capture

Source: Internet
Author: User

The screen is a more commonly used function, the proportion that appears in the project is also relatively high, at least I have done each project has the screen this function, from the full screen to the area has appeared. Of course the area already contains full screen.

There are several ways to make a full screen, invoke the API, invoke the operating system, and go to the Clipboard (which is of course almost no one will use), draw a screen with a graphics, and so on.

The following is the code of the graphics screen, after all, this way the least amount of code.

?
1 2 3 4 5 //intercept screen bitmap myimage = new bitmap (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); graphics g = graphics.fromimage (myimage); g.copyfromscreen ( new point (0, 0), new point (0, 0), new size (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));         g.dispose ();

The above code can intercept the image of the current screen. Note: Because WPF is rendered using DirectX, this method is not available in parts of the system to WPF forms. (Win7,windows server2008,windows Vista can intercept WPF forms, Windows XP cannot intercept WPF forms, possibly because Windows XP does not have WPF yet)

Implementation area, there are four main steps, a capture full screen, pop mask layer, draw selection area, intercept selection area

First, the interception of full-screen can be implemented in the above code, here is not written.

Second, eject the mask layer

The purpose of the mask layer is to draw the area, generally in a translucent way. The code is as follows:

?
1 2 3 4 this.BackColor = Color.Gray; this.Opacity = 0.5; this.FormBorderStyle = FormBorderStyle.None;this.WindowState = FormWindowState.Maximized;

Set the form background color, visibility, border style and maximize it. You can eject the full-screen mask layer.

Three, the painting selection area

This is the most important part of the screenshot.

We need to add the PictureBox control to the form, and the Dock property is set to fill so that the selection can be drawn on the screen

follow the mouse to draw a rectangle on the PictureBox picturePrivate intIntstartx =0; Private intIntstarty =0; Private BOOLIsmousedraw =false; Private voidPicturebox_src_mousedown (Objectsender, MouseEventArgs e) {Ismousedraw=true; Intstartx=e.x; Intstarty=e.y; }         Private voidPicturebox_src_mousemove (Objectsender, MouseEventArgs e) {            if(Ismousedraw) {Try{Graphics g= This. Picturebox_src.creategraphics (); //clear the last picture.G.clear ( This. Picturebox_src.backcolor); Brush Brush=NewSolidBrush (color.red); Pen Pen=NewPen (Brush,1); Pen. DashStyle=Dashstyle.solid; G.drawrectangle (pen,NewRectangle (Intstartx > E.x e.x:intstartx, Intstarty > e.y? e.y:intstarty, Math.Abs (E.X-INTSTARTX), Math.Abs ( E.Y-( intstarty)));                G.dispose (); }                Catch(Exception ex) {ex.                ToString (); }            }        }         Private voidPicturebox_src_mouseup (Objectsender, MouseEventArgs e) {Ismousedraw=false; Intstartx=0; Intstarty=0; }

Of course, this is just a picture of a red rectangle, can not adjust the size of the rectangle two times, moving the rectangle, and paint will not stop to clear the mark, causing the screen to blink, and the usual screen in the selection area of the brightness will be higher, these are not realized, but since can draw the area that these are not problems, Just a little change, here are some of the code to modify:

Fill the selection area

?
1 2 3 4 SolidBrush soldwhite = new SolidBrush(Color.White); Rectangle rec = new Rectangle(StartX, StartY, ScreenWidth, ScreenHeight);g.FillRectangle(soldwhite, rec);//用来填充矩形区域

Screen flashing

When you clear the screen flashes because the entire picture is cleared and then redrawn, so that if the fill area is set to white, we can see that the fill area is flashing very strong, the solution is to fill the way out of the selection area, so that the entire picture is divided by 5 of this fill and one dash.

?
1 2 3 4 5 6 7 Graphics g = this.pictureBox1.CreateGraphics();         //清空上次画下的痕迹        // g.Clear(this.pictureBox1.BackColor);         g.FillRectangle(soldgray, 0, 0, pictureBox1.Width, StartY);//清除上         g.FillRectangle(soldgray, 0, StartY + ScreenHeight, pictureBox1.Width, pictureBox1.Height - (StartY + ScreenHeight));//清除下         g.FillRectangle(soldgray, 0, StartY, StartX, ScreenHeight + 1);//清除左         g.FillRectangle(soldgray, StartX + ScreenWidth, StartY, pictureBox1.Width - (StartX + ScreenWidth), ScreenHeight + 1);//清除右

Secondary sizing and selection movement

When we make a selection of the area, we usually see a change in the mouse style two times, indicating that it can be modified or moved

        /// <summary>        ///Setting the mouse style/// </summary>        /// <param name= "p" ></param>        Private voidSetcursorstyle (point p) {if(p.x > StartX && p.x < StartX + screenwidth && p.y > Starty && p.y < Starty +screenheight) { This. Cursor =Cursors.sizeall; }            Else if(p.x >= StartX-Ten&& p.x <= StartX && p.y >= starty-Ten&& p.y <=starty) { This. Cursor =Cursors.sizenwse; }            Else if(p.x >= StartX + screenwidth && p.x <= StartX + screenwidth +Ten&& p.y <= starty + screenheight +Ten&& p.y >= Starty +screenheight) { This. Cursor =Cursors.sizenwse; }            Else if((p.x >= StartX + screenwidth && p.x <= StartX + screenwidth +Ten&& p.y >= Starty-Ten&& p.y <=starty)) { This. Cursor =CURSORS.SIZENESW; }            Else if(p.x >= StartX-Ten&& p.x <= StartX && p.y <= starty + screenheight +Ten&& p.y >= Starty +screenheight) { This. Cursor =CURSORS.SIZENESW; }            Else            { This. Cursor =Cursors.Default; }        }

This is to set the mouse in the selection area of the style and four corner of the style, if in 4 corners, just set the starting coordinates after the same as the original drawing area on the line, if in the area, the mobile selection will need to calculate the travel value according to the current coordinates and the starting coordinates, change the starting coordinates when moving, fixed the width high picture If you move to the edge of the screen, add judgment.

Iv. Interception of selected areas

After drawing the area, you need to choose whether to intercept, usually give the button selection we simply add the MouseUp and MouseDown events to the hidden and display of the button, the display will calculate the coordinates displayed according to the selection area.

Intercepting the selection area code

?
1 Bitmap map = myImage.Clone(newRectangle(StartX, StartY, ScreenWidth, ScreenHeight), System.Drawing.Imaging.PixelFormat.Format32bppArgb);

This allows for a simple area to be implemented.

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.