C # screenshot code

Source: Internet
Author: User
// 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 );}}


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.