Intercepting computer screens with C #

Source: Internet
Author: User
Tags range

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&GT;XSCRN) nx2 = XSCRN;
if (NY2&GT;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
);



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.