C # screen capture

Source: Internet
Author: User

Using system;
Using system. runtime. interopservices;
Using system. drawing;
Using system. Drawing. imaging;

Namespace screenshotdemo
{
/// <Summary>
/// Capture full screen or an irregular Window Function and save it.
/// </Summary>
Public class screencapture
{
/// <Summary>
/// Creates an image object containing a screen shot of the entire desktop?
/// </Summary>
/// <Returns> </returns>
Public Image capturescreen ()
{
Return capturewindow (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>
Public 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
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, gdi32.srccopy );
// Restore Selection
Gdi32.selectobject (hdcdest, hold );
// Clean up
Gdi32.deletedc (hdcdest );
User32.releasedc (handle, hdcsrc );

// Get A. Net image object for it
Image IMG = image. fromhbitmap (hbitmap );
// Free up the bitmap object
Gdi32.deleteobject (hbitmap );

Return IMG;
}

///


// captures a screen shot of a specific window, and saves it to a file?
///
///
///
//
Public void capturewindowtofile (intptr handle, string filename, imageformat format)
{< br> 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 void capturescreentofile (string filename, imageformat format)
{
Image IMG = capturescreen ();
IMG. Save (filename, format );
}

/// <Summary>
/// Helper class containing GDI32 API functions
/// </Summary>
Private class GDI32
{

Public const int srccopy = 0x00cc0020; // bitblt dwrop Parameter

[Dllimport ("gdi32.dll")]
Public static extern bool bitblt (intptr hobject, int nxdest, int nydest,
Int nwidth, int nheight, intptr hobjectsource,
Int nxsrc, int nysrc, int dwrop );
[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 getasktopwindow ();
[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 );

}

}
}

Related Article

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.