C#截取屏幕

 
     
/// <summary>
/// 截取屏幕
/// </summary>
class ScreenGrab
{
#region Functional imports for ScreenGrab functionality
[DllImport(
" GDI32.dll " )]
public static extern bool BitBlt( int hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, int hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport(
" GDI32.dll " )]
public static extern int CreateCompatibleBitmap( int hdc, int nWidth, int nHeight);
[DllImport(
" GDI32.dll " )]
public static extern int CreateCompatibleDC( int hdc);
[DllImport(
" GDI32.dll " )]
public static extern bool DeleteDC( int hdc);
[DllImport(
" GDI32.dll " )]
public static extern bool DeleteObject( int hObject);
[DllImport(
" GDI32.dll " )]
public static extern int GetDeviceCaps( int hdc, int nIndex);
[DllImport(
" GDI32.dll " )]
public static extern int SelectObject( int hdc, int hgdiobj);
[DllImport(
" User32.dll " )]
public static extern int GetDesktopWindow();
[DllImport(
" User32.dll " )]
public static extern int GetWindowDC( int hWnd);
[DllImport(
" User32.dll " )]
public static extern int ReleaseDC( int hWnd, int hDC);
#endregion

// Captures the current on-screen representation using Windows API calls
public Bitmap CaptureScreen()
{
// Provides a pointer to the visual representation of the desktop window
int source = GetWindowDC(GetDesktopWindow());
// Secures the image using CreateCompatibleBitmap
int bitmap = CreateCompatibleBitmap(source, GetDeviceCaps(source, 8 ), GetDeviceCaps(source, 10 ));

int destination = CreateCompatibleDC(source);

SelectObject(destination, bitmap);
BitBlt(destination,
0 , 0 , GetDeviceCaps(source, 8 ), GetDeviceCaps(source, 10 ), source, 0 , 0 , 0x00CC0020 );
Bitmap image
= GetImage(bitmap);
Cleanup(bitmap, source, destination);
return image;
}

private void Cleanup( int bitmap, int source, int destination)
{
ReleaseDC(GetDesktopWindow(), source);
DeleteDC(destination);
DeleteObject(bitmap);
}

private Bitmap GetImage( int hBitmap)
{
Bitmap image
= new Bitmap(Image.FromHbitmap( new IntPtr(hBitmap)), Image.FromHbitmap( new IntPtr(hBitmap)).Width,
Image.FromHbitmap(
new IntPtr(hBitmap)).Height);
return image;
}
}

转载于:https://www.cnblogs.com/EasyData/archive/2008/09/05/1285339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值