Capture a screen Shot

Source: Internet
Author: User

usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespacescreenshotdemo{/// <summary>    ///provides functions to capture the entire screens, or a particular window, and save it to a file. /// </summary>     Public classScreencapture {/// <summary>        ///creates an Image object containing a screen shot of the entire desktop/// </summary>        /// <returns></returns>         PublicImage Capturescreen () {returnCaptureWindow (User32.getdesktopwindow ()); }        /// <summary>        ///creates an Image object containing a screen shot of a specific window/// </summary>        /// <param name= "Handle" >The handle to the window. (In Windows Forms, this is obtained by the Handle property)</param>        /// <returns></returns>         PublicImage CaptureWindow (IntPtr handle) {//get Te HDC of the target windowIntPtr HDCSRC =USER32.GETWINDOWDC (handle); //Get the sizeUser32.rect Windowrect =NewUser32.rect (); User32.getwindowrect (Handle,refwindowrect); intwidth = windowrect.right-Windowrect.left; intHeight = windowrect.bottom-Windowrect.top; //Create a device context we can copy toIntPtr hdcdest =GDI32.            CreateCompatibleDC (HDCSRC); //Create a bitmap we can copy it to,//using GetDeviceCaps to get the Width/heightIntPtr Hbitmap =GDI32.             CreateCompatibleBitmap (Hdcsrc,width,height); //Select the Bitmap objectIntPtr hold =GDI32.            SelectObject (HDCDEST,HBITMAP); //BitBlt overGDI32. BitBlt (Hdcdest,0,0, WIDTH,HEIGHT,HDCSRC,0,0, GDI32.            SRCCOPY); //Restore SelectionGDI32.            SelectObject (Hdcdest,hold); // Clean UpGDI32.            DeleteDC (hdcdest);            User32.releasedc (HANDLE,HDCSRC); //get a. NET image object for itImage img =Image.fromhbitmap (HBITMAP); //Free up the Bitmap objectGDI32.            DeleteObject (HBITMAP); returnimg; }        /// <summary>        ///Captures A screen shot of a specific windows, and saves it to a file/// </summary>        /// <param name= "Handle" ></param>        /// <param name= "filename" ></param>        /// <param name= "format" ></param>         Public voidCapturewindowtofile (INTPTR handle,stringfilename, imageformat format) {Image img=CaptureWindow (handle); Img.        Save (Filename,format); }        /// <summary>        ///Captures A screen shot of the entire desktop, and saves it to a file/// </summary>        /// <param name= "filename" ></param>        /// <param name= "format" ></param>         Public voidCapturescreentofile (stringfilename, imageformat format) {Image img=Capturescreen (); Img.        Save (Filename,format); }              /// <summary>        ///Helper class containing Gdi32 API functions/// </summary>        Private classGDI32 { Public Const intSrccopy =0x00cc0020;//BitBlt dwrop parameter[DllImport ("Gdi32.dll")]             Public Static extern BOOLBitBlt (IntPtr hobject,intNxdest,intNydest,intNwidth,intnheight,intptr Hobjectsource,intNXSRC,intNYSRC,intdwrop); [DllImport ("Gdi32.dll")]             Public Static externIntPtr CreateCompatibleBitmap (IntPtr HDC,intnwidth,intnheight); [DllImport ("Gdi32.dll")]             Public Static externIntPtr CreateCompatibleDC (IntPtr HDC); [DllImport ("Gdi32.dll")]             Public Static extern BOOLDeleteDC (IntPtr HDC); [DllImport ("Gdi32.dll")]             Public Static extern BOOLDeleteObject (IntPtr hobject); [DllImport ("Gdi32.dll")]             Public Static externIntPtr SelectObject (IntPtr hdc,intptr hobject); }        /// <summary>        ///Helper class containing User32 API functions/// </summary>        Private classUser32 {[StructLayout (layoutkind.sequential)] Public structRECT { Public intLeft ;  Public inttop;  Public intRight ;  Public intBottom; } [DllImport ("user32.dll")]             Public Static externIntPtr GetDesktopWindow (); [DllImport ("user32.dll")]             Public Static externIntPtr GETWINDOWDC (IntPtr hWnd); [DllImport ("user32.dll")]             Public Static externIntPtr ReleaseDC (IntPtr hwnd,intptr HDC); [DllImport ("user32.dll")]             Public Static externIntPtr GetWindowRect (IntPtr hWnd,refrect rect); }    }}

Capture a screen Shot

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.