Call Win32API to capture the source code of the screen or the current active window

zhaozj2021-02-16  52

This article uses GDI32.dll to implement capture screens or current active windows. All APIs run in "kernel", "user" and "gdi" three libraries: where "kernel", his library is "kernel32.dll", which is mainly used to generate associations with the operating system For example: program loading, context selection, file input, memory management, etc. "User" library is called "user32.dll" in Win32. It allows you to manage all user interfaces. Such as: windows, menus, dialogs, icons, etc. "GDI" (image device interface), its library in Win32 is: "gdi32.dll", it is a graphics output library. Use the GDI Windows "Draw" window, menu, and dialog, etc .; it can create graphics output; it can also save graphics files.

We use in this article

API

Function is

"Bitblt"

The introduction is as follows:

Function function: This function converts the pixels in the specified source device area (bit_block) to transfer to the target device environment. Function prototype: BOOL Bitblt (HDC HDCDest, Int NXDEST, INT NYDEST, INT NWIDTH, INT NHEIGHT, HDC HDCSRC, INT NXSRC, INT NYSRC, DWORD DWROP); Parameters: HDCDest: Presses the handle of the target device environment. NXDEST: Specifies the X-axis logic coordinates in the upper left corner of the target rectangle area. NYDEST: Specifies the Y-axis logic coordinates in the upper left corner of the target rectangle area. NWIDTH: Specifies the logical width of the source and target rectangular area. NHEIGHT: Specifies the logic height of the source and target rectangular area. HDCSRC: Points the handle of the source device environment. NXSRC: Specifies the x-axis logic coordinates in the upper left corner of the source rectangle area. NYSRC: Specifies the Y-axis logic coordinates in the upper left corner of the source rectangle area. DWROP: Specifies the grating operation code. These codes will define color data of the source rectangular area, how to combine the color data of the target rectangular area to complete the last color.

The method is called in the C # in the source code below. A ScreenCapture class is defined here, with a static method GetScreenMap (Graphics G1, Rectangle Rect).

//Screencapture.cs

// author: Hetonghai 2004/06/23

Namespace screencapture {

Using system;

Using system.Runtime.InteropServices;

Using system.windows.forms;

Using system.drawing;

///

/// Claw the class of the current screen

///

Public class screencapture {

protected screencapture () {

;

}

///

/// Declare an API function

/// Function: This function converts the pixels in the specified source device to the target device environment.

/// Function prototype: Bool Bitblt (HDC HDCDest, Int NXDEST, INT NYDEST, INT NWIDTH, INT NHEIGHT, HDC HDCSRC, INT NXSRC, INT NYSRC, DWORD DWROP); ///

/// HDCDEST: Points the handle of the target device environment.

/// NXDEST: Specifies the X-axis logic coordinates in the upper left corner of the target rectangle area.

/// NYDEST: Specifies the Y-axis logic coordinates in the upper left corner of the target rectangle area.

/// NWIDTH: Specify the logical width of the source and target rectangular area.

/// NHEight: Specify the logic height of the source and target rectangular area.

/// HDCSRC: Points the handle of the source device environment.

/// nxsrc: Specifies the X-axis logic coordinates in the upper left corner of the source rectangle area.

/// NYSRC: Specifies the Y-axis logic coordinates in the upper left corner of the source rectangular area.

/// DWROP: Specifies the grating operation code. These codes will define color data of the source rectangular area, how to combine the color data of the target rectangular area to complete the last color.

/// Return Value: If the function is successful, the return value is non-zero; if the function fails, the return value is zero.

[System.Runtime.InteropServices.dllimportattribute ("gdi32.dll")]]]]

Private static extern bool bitblt

INTPTR HDCDEST, / / ​​Target DC handle

Int nxdest,

Int Nydest,

Int nwidth,

Int nheight,

INTPTR HDCSRC, // Source DC handle

Int nxsrc,

Int Nysrc,

System.Int32 DWROP / / Raster processing value

);

///

/// Create an image of the RECT range with the current screen

///

/// Define the scope of the screen

///

Public Static Image GetScreenMap (Graphics G1, Rectangle Rect) {

// Create a image with the current screen as a template

// graphics g1 = this.creategraphics ();

// Create a bitmap based on screen size

Image myimage = new bitmap (Rect.width, Rect.Height, G1); Graphics G2 = graphics.fromimage (MyImage);

// Get the DC of the screen

INTPTR DC1 = G1.GETHDC ();

// Get the DC of Bitmap

INTPTR DC2 = G2.GETHDC ();

// Call this API function and implement the screen capture

Bitblt (DC2, 0, 0, Rect.width, Rect.height, DC1, Rect.x, Rect.y, 13369376);

/ / Release the DC of the screen

G1.ReleaseHDC (DC1);

/ / Release the DC of Bitmap

g2.releaseHDC (DC2);

Return myimage;

}

}

}

The source code of the call is given below. Private string savefile (Rectangle Rect) {

Graphics g1 = this.creategraphics ();

Image myimg = screencapture.getscreenmap (g1, rect);

String path = this.txtdirectory.Text.trim ();

String filter = path.substring (path.length-3, 3);

String Ret = "OK";

Switch (Filter.tolower ()) {

Case "JPG": {

MyImg.save (path, imageformat.jpeg);

Break;

}

Case "BMP": {

MyIMg.save (path, imageformat.bmp);

Break;

}

Case "gif": {

MyImg.save (path, imageformat.gif);

Break;

}

DEFAULT: {

Ret = "error";

Break;

}

}

Return Ret;

}

Calling method: private void btnsavecurrent_click (Object sender, system.eventargs e) {

String path = this.txtdirectory.Text.trim ();

Rectangle Rect = this.RectangletoClient (New Rectangle (this.width, this.Height);

IF (Savefile (RECT) == "OK") {

Messagebox.show ("Save as" PATH "file success!");

}

Else {

Messagebox.show ("Save as" Path "file failed!");

}

转载请注明原文地址:https://www.9cbs.com/read-20783.html

New Post(0)