Today, I'm bored with turning code out a previously written C # screenshot function... share it with you. this code was rewritten by referring to the C ++ code of a screenshot on the Internet. I just declared all the APIs. the declared APIs are also attached. for reference. if you have any questions, please contact us. (Appledotnet@hotmail.com)
///
/// Capture part of the screen
///
/// Upper left corner
/// Bottom right corner
/// Whether the screen is full
/// 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 = CreateCompatibleDC (hscrdc); // create a memory DC
Xscrn = GetDeviceCaps (hscrdc, GetDeviceCapsIndex. HORZRES); // obtain the screen width
Yscrn = GetDeviceCaps (hscrdc, GetDeviceCapsIndex. VERTRES); // obtain the screen height
If (Full) // capture 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 value Validity
If (nx <0) nx = 0;
If (ny <0) ny = 0;
If (nx2> xscrn) nx2 = xscrn;
If (ny2> yscrn) ny2 = yscrn;
}
Nwidth = nx2-nx; // width of the truncation range
Nheight = ny2-ny; // height of the screenshot 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); // deletes a used object.
DeleteDC (hmemdc); // delete an object that has been used
Return Bitmap. FromHbitmap (hbitmap); // use Bitmap. FromHbitmap to return Bitmap from hbitmap
}