C # screen capture [including desktop and window]

Source: Internet
Author: User

It is relatively simple to intercept desktop images. You can simply call graphics's copyfromscreen method;

 

It is easy to draw an image to a window, but few articles show that the image is captured from a window. The following describes how to intercept window images.

The bitblt function provided by the system is used to capture the image of the window. This function is used to copy a bitmap image from the context of the source device to the target device. For more information about the parameters, see the msdn documentation.

The following is the C # function introduction operation:

[Dllimport ("gdi32.dll", charset = charset. Auto, setlasterror = true, exactspelling = true)]
Public static extern int bitblt (handleref HDC, int X, int y, int nwidth, int nheight, handleref hsrcdc, int xsrc, int ysrc, int dwdrop );

 

The specific operation code is as follows:

Graphics gsrc = This. creategraphics (); // create a graphics object for the form
Handleref hdcsrc = new handleref (null, gsrc. gethdc ());

Int width = This. Width-SystemInformation.FrameBorderSize.Width; // get the width
Int Height = This. Height-SystemInformation.FrameBorderSize.Height; // get height

Const int srccopy = 0xcc0020; // copy the grating operation code of the graph block.

Bitmap bmsave = new Bitmap (width, height); // The bitmap object used to save the image
Graphics gsave = graphics. fromimage (bmsave); // create a graphics object for this bitmap
Handleref hdcsave = new handleref (null, gsave. gethdc (); // get the handle

Bitblt (hdcsave, 0, 0, width, height, hdcsrc, 0, 0, srccopy );

In this way, the image of the current form is saved in bmsave.

 

The following provides a detailed code

 

Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. runtime. interopservices; </P> <p> namespace screenshot <br/> {<br/> Public partial class form1: Form <br/>{< br/> Public form1 () <br/>{< br/> initializecomponent (); <br/>}</P> <p> private Void btnscreen_click (Object sender, eventargs e) <br/>{< br/> int screenwidth = system. windows. forms. systeminformation. virtualscreen. width; // screen width <br/> int screenheight = system. windows. forms. systeminformation. virtualscreen. height; // screen height </P> <p> bitmap bmsave = new Bitmap (screenwidth, screenheight); <br/> graphics G = graphics. fromimage (bmsave); </P> <p> G. copyfromscreen (0, 0, 0, 0, new size (scree Nwidth, screenheight), copypixeloperation. sourcecopy); <br/> G. dispose (); </P> <p> SaveFile (bmsave); <br/>}</P> <p> private void btnwindow_click (Object sender, eventargs E) <br/>{< br/> graphics gsrc = This. creategraphics (); // create the graphics object of the form <br/> handleref hdcsrc = new handleref (null, gsrc. gethdc (); </P> <p> int width = This. width-SystemInformation.FrameBorderSize.Width; // get the width <br/> int Height = This. height-SystemInformation.FrameBorderSize.Height; // get height </P> <p> const int srccopy = 0xcc0020; // copy the grating operation code of the graph block </P> <p> bitmap bmsave = new Bitmap (width, height ); // The bitmap object used to save the image <br/> graphics gsave = graphics. fromimage (bmsave); // create the graphics object for this bitmap <br/> handleref hdcsave = new handleref (null, gsave. gethdc (); // obtain the handle </P> <p> bitblt (hdcsave, 0, 0, width, height, hdcsrc, 0, 0, srccopy ); </P> <p> gsrc. relea Sehdc (); <br/> gsave. releasehdc (); </P> <p> gsrc. dispose (); <br/> gsave. dispose (); </P> <p> SaveFile (bmsave); <br/>}< br/> private void SaveFile (Bitmap bmsave) <br/>{< br/> If (dialogresult. OK = savefiledialog1.showdialog () <br/>{< br/> string filename = savefiledialog1.filename; <br/> if (1 = savefiledialog1.filterindex) <br/>{< br/> If (! Filename. endswith (". BMP ") <br/>{< br/> filename + = ". BMP "; <br/>}< br/> else if (2 = savefiledialog1.filterindex) <br/>{< br/> If (! Filename. endswith (". jpg ") <br/>{< br/> filename + = ". jpg "; <br/>}< br/> else if (3 = savefiledialog1.filterindex) <br/>{< br/> If (! Filename. endswith (". PNG ") <br/>{< br/> filename + = ". PNG "; <br/>}< br/> Save (bmsave, filename ); <br/>}< br/> private void save (Bitmap BM, string filename) <br/>{< br/> If (filename. endswith (". BMP ") <br/>{< br/> BM. save (filename, system. drawing. imaging. imageformat. BMP); <br/>}< br/> else if (filename. endswith (". jpg ") <br/>{< br/> BM. save (filename, system. drawing. imaging. imageformat. JPEG); <br/>}< br/> else if (filename. endswith (". PNG ") <br/>{< br/> BM. save (filename, system. drawing. imaging. imageformat. PNG); <br/>}< br/> BM. dispose (); <br/>}</P> <p> [dllimport ("gdi32.dll", charset = charset. auto, setlasterror = true, exactspelling = true)] <br/> Public static extern int bitblt (handleref HDC, int X, int y, int nwidth, int nheight, handleref hsrcdc, int xsrc, int ysrc, int dwrop); <br/>}< br/>}

 

The code for form design is omitted. The test form image is as follows:

 

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.