Today boring turn code, turn out a previously written C # screenshot function ... Take it out and share it with everyone.
This code is written in reference to a section of the screen in C + + code rewrite. It's just a statement of the API.
The declared APIs are also appended. For reference. If there are any questions, please note.
Author: appledotnet
///
Intercepting part of the screen
///
Upper left corner
Lower right corner
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 DC handle
HMEMDC = CreateCompatibleDC (HSCRDC);//Create a memory DC
XSCRN = GetDeviceCaps (HSCRDC, getdevicecapsindex.horzres);//Get screen width
YSCRN = GetDeviceCaps (HSCRDC, getdevicecapsindex.vertres);//Get screen height
if (full)//If the entire screen is intercepted
{
NX = 0;
NY = 0;
NX2 = XSCRN;
Ny2 = YSCRN;
}
Else
{
NX = P1. X
NY = P1. Y
Nx2 =P2. X
Ny2 =P2. Y
Check numerical validity
if (nx<0) NX = 0;
if (ny<0) NY = 0;
if (NX2>XSCRN) nx2 = XSCRN;
if (NY2>YSCRN) Ny2 = YSCRN;
}
nwidth = width of nx2-nx;//intercept range
nheight = height of ny2-ny;//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 used objects
DeleteDC (HMEMDC);//Delete used objects
Return Bitmap.fromhbitmap (HBITMAP)//Bitmap.fromhbitmap from Hbitmap to Bitmap
}
The API declaration used:
[dllimport ("Gdi32.dll")]
public static extern IntPtr CreateDC (
string lpszdriver, //driver name
string lpszdevice, //Device name
string lpszoutput, //not used; should Be NULL
int64 lpinitdata //optional printer data
;
&NBSP
[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
);