[转]使用C#捕获屏幕图象

我的程序需要捕获屏幕的图象。但是在C#中对这个功能的支持不多。在经过一系列的搜索后,我了解到必须使用Win32 API来完成这个功能。所以我做了下面三个类:

Class Description
PlatformInvokeGDI32 All the GDI32.dll APIs being used in this application are placed in this class.
PlatformInvokeUSER32 All the User32.dll APIs have been placed in this class:
CaptureScreen In this class I have provided a simple static function GetDesktoImage that captures the screen image using the APIs given in PlatformInvokeGDI32 and PlatformInvokeUSER32 and returns it as a bitmap.

你可以容易的把这三个类应用到你的C#程序中,只要把下面的代码拷贝到你的代码中即可,不需要做任何的改变。如果你需要使用源文件,那么你还需要把CaptureScreen名称空间加入到你的工程中或者把在这些类中的CaptureScreen名称空间改为你的工程的名称空间。

漂亮的代码:
///
/// This class shall keep the GDI32 APIs used in our program.
///
public class PlatformInvokeGDI32
{
#region Class Variables
public const int SRCCOPY = 13369376;
#endregion
#region Class Functions
[DllImport("gdi32.dll",EntryPoint="DeleteDC")]
public static extern IntPtr DeleteDC(IntPtr hDc);

[DllImport("gdi32.dll",EntryPoint="DeleteObject")]
public static extern IntPtr DeleteObject(IntPtr hDc);

[DllImport("gdi32.dll",EntryPoint="BitBlt")]
public static extern bool BitBlt(IntPtr hdcDest,int xDest,
int yDest,int wDest,
int hDest,IntPtr hdcSource,
int xSrc,int ySrc,int RasterOp);

[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleBitmap")]
public static extern IntPtr CreateCompatibleBitmap
(IntPtr hdc,int nWidth, int nHeight);

[DllImport ("gdi32.dll",EntryPoint="CreateCompatibleDC")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport ("gdi32.dll",EntryPoint="SelectObject")]
public static extern IntPtr SelectObject(IntPtr hdc,IntPtr bmp);
#endregion

#region Public Constructor
}

///
/// This class shall keep the User32 APIs used in our program.
///
public class PlatformInvokeUSER32
{
#region Class Variables
public const int SM_CXSCREEN=0;
public const int SM_CYSCREEN=1;
#endregion

#region Class Functions
[DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll",EntryPoint="GetDC")]
public static extern IntPtr GetDC(IntPtr ptr);

[DllImport("user32.dll",EntryPoint="GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);

[DllImport("user32.dll",EntryPoint="GetWindowDC")]
public static extern IntPtr GetWindowDC(Int32 ptr);

[DllImport("user32.dll",EntryPoint="ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd,IntPtr hDc);

#endregion
}

///
/// This class shall keep all the functionality for capturing
/// the desktop.
///
public class CaptureScreen
{
#region Class Variable Declaration
protected static IntPtr m_HBitmap;
#endregion

#region Public Class Functions
public static Bitmap GetDesktopImage()
{
//In size variable we shall keep the size of the screen.
SIZE size;

//Here we get the handle to the desktop device context.
IntPtr hDC = PlatformInvokeUSER32.GetDC
(PlatformInvokeUSER32.GetDesktopWindow());

//Here we make a compatible device context in memory for screen
//device context.
IntPtr hMemDC = PlatformInvokeGDI32.CreateCompatibleDC(hDC);

//We pass SM_CXSCREEN constant to GetSystemMetrics to get the
//X coordinates of screen.
size.cx = PlatformInvokeUSER32.GetSystemMetrics
(PlatformInvokeUSER32.SM_CXSCREEN);

//We pass SM_CYSCREEN constant to GetSystemMetrics to get the
//Y coordinates of screen.
size.cy = PlatformInvokeUSER32.GetSystemMetrics
(PlatformInvokeUSER32.SM_CYSCREEN);

//We create a compatible bitmap of screen size and using screen
//device context.
m_HBitmap = PlatformInvokeGDI32.CreateCompatibleBitmap
(hDC, size.cx, size.cy);

//As m_HBitmap is IntPtr we can not check it against null.
//For this purspose IntPtr.Zero is used.
if (m_HBitmap!=IntPtr.Zero)
{
//Here we select the compatible bitmap in memeory device
//context and keeps the refrence to Old bitmap.
IntPtr hOld = (IntPtr) PlatformInvokeGDI32.SelectObject
(hMemDC, m_HBitmap);
//We copy the Bitmap to the memory device context.
PlatformInvokeGDI32.BitBlt(hMemDC, 0, 0,size.cx,size.cy, hDC,
0, 0,PlatformInvokeGDI32.SRCCOPY);
//We select the old bitmap back to the memory device context.
PlatformInvokeGDI32.SelectObject(hMemDC, hOld);
//We delete the memory device context.
PlatformInvokeGDI32.DeleteDC(hMemDC);
//We release the screen device context.
PlatformInvokeUSER32.ReleaseDC(PlatformInvokeUSER32.
GetDesktopWindow(), hDC);
//Image is created by Image bitmap handle and returned.
return System.Drawing.Image.FromHbitmap(m_HBitmap);
}
//If m_HBitmap is null, retun null.
return null;
}
#endregion
}
//This structure shall be used to keep the size of the screen.
public struct SIZE
{
public int cx;
public int cy;
}

这里还有一个例子显示了如何使用这些类。都是很简单的Windows程序,在一个form中加入了一个menu和picture box控件。效果如下图:


希望你喜欢这段代码。

posted @ 2006-12-26 09:03  kiler  阅读(787)  评论(0)    收藏  举报