截取屏幕的类

发布时间:2009年04月22日      浏览次数:877 次
using System;
using System.Drawing;
namespace CaptureScreen
{
/// <summary>
/// CaptureScreen类
/// </summary>
public class CaptureScreen
{
#region Public Class Functions
public static Bitmap GetDesktopImage()
{
//屏幕大小
SIZE size;
//图像.
IntPtr hBitmap;
//User32中的GetDesktopWindow方法
IntPtr hDC = PlatformInvokeUSER32.GetDC(PlatformInvokeUSER32.GetDesktopWindow());
//GDI32中的方法.
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);
//宽度
size.cx = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CXSCREEN);
//长度
size.cy = PlatformInvokeUSER32.GetSystemMetrics(PlatformInvokeUSER32.SM_CYSCREEN);
//创建图像
hBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap(hDC, size.cx, size.cy);
//截屏处理
if (hBitmap != IntPtr.Zero)
{
IntPtr hOld = (IntPtr)PlatformInvokeGDI32.SelectObject(hMemDC, hBitmap);
PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0, size.cx, size.cy, hDC, 0, 0, PlatformInvokeGDI32.SRCCOPY);
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
PlatformInvokeGDI32.DeleteDC(hMemDC);
PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.GetDesktopWindow(), hDC);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
PlatformInvokeGDI32.DeleteObject(hBitmap);
GC.Collect();
//返回图像
return bmp;
}
return null;
}
#endregion
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!