C # intercept the screen ↑

zhaozj2021-02-12  143

Today, I bored the code, turn out a function of a previous written C # screen capture ... I will share it with you. This code is rewritten with the C code of the online screen capture. Only declare the API. Each API is also attached. For reference. If you have any questions, please point out. (AppleDotNet@hotmail.com)

///

/// Intercept some screen

///

///

Upper left corner

///

Lower right corner

///

Whether is full screen

///

Return Value Bitmap

Public Static Bitmap GetPartScreen (Point P1, Point P2, Bool Full)

{

INTPTR HSCRDC, HMEMDC;

INTPTR HBITMAP, HOLDBITMAP;

INT NX, NY, NX2, NY2;

Nx = ny = nx2 = NY2 = 0;

INT NWIDTH, NHEIGHT;

Int xscrn, yscrn;

HSCRDC = CREATEDC ("Display", NULL, NULL, 0); // Create a DC handle

HMEMDC = CREATECOMPATIPLEDC (HSCRDC); // Create a memory DC

Xscrn = getDeviceCaps (HSCRDC, GetDeviceCapsindex.horzres); // Get screen width

YSCRN = getDeviceCaps (HSCRDC, getDeviceCapsindex.vertres); // Get the screen height

IF (full) // If it is intercepted the entire screen

{

Nx = 0;

NY = 0;

NX2 = xscrn;

NY2 = YSCRN;

}

Else

{

Nx = p1.x;

NY = p1.y;

NX2 = p2.x;

NY2 = p2.y;

/ / Check the legitimacy of numerical

IF (NX <0) nx = 0;

IF (NY <0) ny = 0;

IF (Nx2> xscrn) nx2 = xscrn;

IF (NY2> YSCRN) NY2 = YSCRN;

}

NWIDTH = NX2 - NX; // The width of the intercept range

NHEIGHT = NY2 - NY; / / The height of the intercept range

hbitmap = createcompatiblebitmap (HSCRDC, NWIDTH, NHEIGHT); // Copy from memory DC to HBitmap handle

HoldbitMap = SELECTOBJECT (HMEMDC, HBitmap);

Bitblt (HMEMDC, 0, 0, NWIDTH, NHEIGHT, HSCRDC, NX, NY, (UINT32) 0XCC0020);

Hbitmap = selectObject (HMEMDC, Holdbitmap);

DeleteDC (HSCRDC); // Delete the used object

Deletedc (hmemdc); // Delete the used object

Return bitmap.fromhbitmap (hbitmap); // Return Bitmap from Hbitmap with BitMap.Fromhbitmap

}

The API used is declared:

[DllImport ( "gdi32.dll")] public static extern IntPtr CreateDC (string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL UInt32 lpInitData // optional printer data); [DllImport ( "gdi32.dll")] public static extern IntPtr CreateCompatibleDC (IntPtr hdc // handle to DC); [DllImport ( "gdi32.dll")] public static extern int GetDeviceCaps (IntPtr hdc, // handle to DC GetDeviceCapsIndex nIndex // index of capability); [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 Bitblt (INTPTR HDCDEST, // Handle to Destination DC INT NXDEST, // X-Coord of Destination Upper-Left Corner Int Nydest, // Y-Coord of Destination Upper -left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y -Coordinate of Source Upper-Left Corner Uint32 DWrop // Raster Operation Code);

[DLLIMPORT ("gdi32.dll")] public static extern int deletedc (INTPTR HDC // Handle to DC);

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

New Post(0)