// Method 1 public static void getscreen1 () {// capture screen content size screen = new size (screen. primaryscreen. bounds. width, screen. primaryscreen. bounds. height); bitmap memoryimage = new Bitmap (screen. width, screen. height); graphics memorygraphics = graphics. fromimage (memoryimage); memorygraphics. copyfromscreen (0, 0, 0, 0, screen, copypixeloperation. mergepaint); // memoryimage. save (@ "screen.jpg", imageformat. JPEG); memorystream DATA = new memorystream (); memoryimage. save (data, imageformat. PNG );}
// Method 2 public class screencapture {// <summary> // creates an image object containing a screen shot of the entire desktop /// </Summary> // <returns> </returns> Public static image capturescreen () {return capturewindow (user32.getdesktopwindow ());} /// <summary> // creates an image object containing a screen shot of a specific window // </Summary> /// <Param name = "handle"> handle to the window. (In Windows Forms, this is obtained by the handle property) </param> // <returns> </returns> Private Static Image capturewindow (intptr handle) {// get te HDC of the target window intptr hdcsrc = user32.getwindowdc (handle); // get the size user32.rect windowrect = new user32.rect (); user32.getwindowrect (handle, ref windowrect ); int width = windowrect. right-windowrect. left; int Height = windowrect. bottom-windowrect. top; // create a device context we can copy to intptr hdcdest = gdi32.createcompatibledc (hdcsrc); // create a bitmap we can copy it, // using getdevicecaps to get the width/height intptr hbitmap = gdi32.createcompatiblebitmap (hdcsrc, width, height); // select the bitmap object intptr hold = gdi32.selectobject (hdcdest, hbitmap ); // bitblt over gdi32.bitblt (hdcdest, 0, 0, width, height, hdcsrc, 0, 0, gdi32.srccopy | duplicate); // restore selection gdi32.selectobject (hdcdest, hold ); // clean up gdi32.deletedc (hdcdest); user32.releasedc (handle, hdcsrc); // get. net image object for it image IMG = image. fromhbitmap (hbitmap); // free up the bitmap object gdi32.deleteobject (hbitmap); Return IMG ;} /// <summary> /// helper class containing GDI32 API functions // </Summary> private class GDI32 {public const int captureblt = 1073741824; Public const int srccopy = 0x00cc0020; // bitblt dwrop parameter [dllimport ("failed")] public static extern bool bitblt (intptr hobject, int nxdest, int nydest, int nwidth, int nheight, intptr hobjectsource, int nxsrc, int nysrc, int dwdrop); [dllimport ("gdi32.dll")] public static extern intptr createcompatiblebitmap (intptr HDC, int nwidth, int nheight); [dllimport ("gdi32.dll")] public static extern intptr createcompatibledc (intptr HDC); [dllimport ("gdi32.dll")] public static extern bool deletedc (intptr HDC); [dllimport ("gdi32.dll")] public static extern bool deleteobject (intptr hobject); [dllimport ("gdi32.dll")] public static extern intptr SelectObject (intptr HDC, intptr hobject );} /// <summary> /// helper class containing USER32 API functions // </Summary> private class USER32 {[structlayout (layoutkind. sequential)] public struct rect {public int left; Public int top; Public int right; Public int bottom;} [dllimport ("user32.dll")] public static extern intptr get1_topwindow (); [dllimport ("user32.dll")] public static extern intptr getwindowdc (intptr hwnd); [dllimport ("user32.dll")] public static extern intptr releasedc (intptr hwnd, intptr HDC ); [dllimport ("user32.dll")] public static extern intptr getwindowrect (intptr hwnd, ref rect );}}