Screenshots of non-top windows

Source: Internet
Author: User
How to intercept non-front-end Forms

First, let's talk about the use of the printwindow API.

Public static bitmap getwindowcapture (intptr hwnd)
{
Intptr hscrdc = getwindowdc (hwnd );
Rect windowrect = new rect ();
Getwindowrect (hwnd, ref windowrect );
Int width = windowrect. Right-windowrect. Left;
Int Height = windowrect. Bottom-windowrect. Top;

Intptr hbitmap = createcompatiblebitmap (hscrdc, width, height );
Intptr hmemdc = createcompatibledc (hscrdc );
SelectObject (hmemdc, hbitmap );
Printwindow (hwnd, hmemdc, 0 );
Bitmap BMP = bitmap. fromhbitmap (hbitmap );
Deletedc (hscrdc); // deletes a used object.
Deletedc (hmemdc); // delete an object that has been used
Return BMP;
}

[Dllimport ("user32.dll")]
Public static extern intptr getwindowrect (intptr hwnd, ref rect );

[Dllimport ("gdi32.dll")]
Public static extern intptr createdc (
String lpszdriver, // driver name
String lpszdevice, // device name
String lpszoutput, // not used; shocould be null
Intptr lpinitdata // optional printer data
);
[Dllimport ("gdi32.dll")]
Public static extern int bitblt (
Intptr hdcdest, // handle of the target device to destination DC
Int nxdest, // X-coord of destination upper-left corner X coordinate in the upper left corner of the target object
Int nydest, // y-coord of destination upper-left corner y coordinate in the upper left corner of the target object
Int nwidth, // width of destination rectangle the width of the rectangle of the target object
Int nheight, // height of destination rectangle specifies the length of the rectangle of the target object.
Intptr hdcsrc, // handle to source DC Source Device
Int nxsrc, // X-coordinate of source upper-left corner X coordinate in the upper left corner of the source object
Int nysrc, // y-coordinate of source upper-left corner y coordinate in the upper left corner of the source object
Uint32 dwrop // operation value of raster operation code Grating
);

[Dllimport ("gdi32.dll")]
Public static extern intptr createcompatibledc (
Intptr HDC // handle to DC
);

[Dllimport ("gdi32.dll")]
Public static extern intptr createcompatiblebitmap (
Intptr HDC, // handle to DC
Int nwidth, // width of bitmap, in pixels
Int nheight // height of bitmap, in pixels
);

[Dllimport ("gdi32.dll")]
Public static extern intptr SelectObject (
Intptr HDC, // handle to DC
Intptr hgdiobj // handle to object
);

[Dllimport ("gdi32.dll")]
Public static extern int deletedc (
Intptr HDC // handle to DC
);

[Dllimport ("user32.dll")]
Public static extern bool printwindow (
Intptr hwnd, // window to copy, handle to the window that will be copied.
Intptr hdcblt, // HDC to print into, handle to the device context.
Uint32 nflags // optional flags, specifies the drawing options. It can be one of the following values.
);

[Dllimport ("user32.dll")]
Public static extern intptr getwindowdc (
Intptr hwnd
);

 

 

Unfortunately, the above can indeed intercept non-front-end forms, but non-GDI programs cannot, such as DirectX

The following describes the use of bitblt APIs.

 

/// <Summary>
/// Provides full screen and specified Windows and classes for saving as files
/// </Summary>
Public class screencapture
{
/// <Summary>
/// Full screen
/// </Summary>
/// <Returns> </returns>
Public Image capturescreen ()
{
Return capturewindow (user32.getdesktopwindow ());
}
/// <Summary>
/// Specify the window
/// </Summary>
/// <Param name = "handle"> window handle. (obtained from the handle attribute in a Windows Application) </param>
/// <Returns> </returns>
Public Image capturewindow (intptr handle)
{
Intptr hdcsrc = user32.getwindowdc (handle );
User32.rect windowrect = new user32.rect ();
User32.getwindowrect (handle, ref windowrect );
Int width = windowrect. Right-windowrect. Left;
Int Height = windowrect. Bottom-windowrect. Top;
Intptr hdcdest = gdi32.createcompatibledc (hdcsrc );
Intptr hbitmap = gdi32.createcompatiblebitmap (hdcsrc, width, height );
Intptr hold = gdi32.selectobject (hdcdest, hbitmap );
Gdi32.bitblt (hdcdest, 0, 0, width, height, hdcsrc, 0, 0, gdi32.srccopy );
Gdi32.selectobject (hdcdest, hold );
Gdi32.deletedc (hdcdest );
User32.releasedc (handle, hdcsrc );
Image IMG = image. fromhbitmap (hbitmap );
Gdi32.deleteobject (hbitmap );
Return IMG;
}
/// <Summary>
/// Specify the window to save as an image file
/// </Summary>
/// <Param name = "handle"> </param>
/// <Param name = "FILENAME"> </param>
/// <Param name = "format"> </param>
Public void capturewindowtofile (intptr handle, string filename, imageformat format)
{
Image IMG = capturewindow (handle );
IMG. Save (filename, format );
}
/// <Summary>
/// Save as a file in full screen
/// </Summary>
/// <Param name = "FILENAME"> </param>
/// <Param name = "format"> </param>
Public void capturescreentofile (string filename, imageformat format)
{
Image IMG = capturescreen ();
IMG. Save (filename, format );
}

/// <Summary>
/// The helper class defines the GDI32 API Function
/// </Summary>
Private class GDI32
{

Public const int srccopy = 0x00cc0020;
[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>
/// The auxiliary class defines the USER32 API Function
/// </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 );
}
}

 

 

The above class uses the bitblt API to intercept GDI or non-GDI graphics, but non-front-end form graphics cannot intercept...

The following describes the simplest full screen solution without using APIs.

 

Public static bitmap copyprimaryscreen ()
{
Screen S = screen. primaryscreen;
Rectangle r = S. bounds;
Int W = R. width;
Int H = R. height;
Bitmap BMP = new Bitmap (W, H );
Graphics G = graphics. fromimage (BMP );
G. copyfromscreen
(
New Point (0, 0 ),
New Point (0, 0 ),
New size (W, H)
);
Return BMP;
}

 

Finally, try the following method:

 

As long as the visable of the form is true, a graph can be captured even outside the screen. If it is false, it is a black image, hehe.
 
Public static bitmap getwindow (intptr hwnd)
{
Intptr hscrdc = getwindowdc (hwnd );
Control control = control. fromhandle (hwnd );
Intptr hbitmap = createcompatiblebitmap (hscrdc, Control. Width, Control. Height );
Intptr hmemdc = createcompatibledc (hscrdc );
SelectObject (hmemdc, hbitmap );
Printwindow (hwnd, hmemdc, 0 );
Bitmap BMP = bitmap. fromhbitmap (hbitmap );
Deletedc (hscrdc); // deletes a used object.
Deletedc (hmemdc); // delete an object that has been used
Return BMP;
}

API Declaration

[Dllimport ("gdi32.dll")]
Public static extern intptr createdc (
String lpszdriver, // driver name
String lpszdevice, // device name
String lpszoutput, // not used; shocould be null
Intptr lpinitdata // optional printer data
);
[Dllimport ("gdi32.dll")]
Public static extern int bitblt (
Intptr hdcdest, // handle of the target device to destination DC
Int nxdest, // X-coord of destination upper-left corner X coordinate in the upper left corner of the target object
Int nydest, // y-coord of destination upper-left corner y coordinate in the upper left corner of the target object
Int nwidth, // width of destination rectangle the width of the rectangle of the target object
Int nheight, // height of destination rectangle specifies the length of the rectangle of the target object.
Intptr hdcsrc, // handle to source DC Source Device
Int nxsrc, // X-coordinate of source upper-left corner X coordinate in the upper left corner of the source object
Int nysrc, // y-coordinate of source upper-left corner y coordinate in the upper left corner of the source object
Uint32 dwrop // operation value of raster operation code Grating
);

[Dllimport ("gdi32.dll")]
Public static extern intptr createcompatibledc (
Intptr HDC // handle to DC
);

[Dllimport ("gdi32.dll")]
Public static extern intptr createcompatiblebitmap (
Intptr HDC, // handle to DC
Int nwidth, // width of bitmap, in pixels
Int nheight // height of bitmap, in pixels
);

[Dllimport ("gdi32.dll")]
Public static extern intptr SelectObject (
Intptr HDC, // handle to DC
Intptr hgdiobj // handle to object
);

[Dllimport ("gdi32.dll")]
Public static extern int deletedc (
Intptr HDC // handle to DC
);

[Dllimport ("user32.dll")]
Public static extern bool printwindow (
Intptr hwnd, // window to copy, handle to the window that will be copied.
Intptr hdcblt, // HDC to print into, handle to the device context.
Uint32 nflags // optional flags, specifies the drawing options. It can be one of the following values.
);

[Dllimport ("user32.dll")]
Public static extern intptr getwindowdc (
Intptr hwnd
);

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.