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