最近在嘗試為軟件增加截取屏幕的功能,為此學習了System.Drawing命名空間的Graphics、Image、Bitmap等GDI+類,這些類都很方便使用。但是它們大多都是對原有GDI API的封裝,也增加了一些新的東西;不過封裝得并不徹底,有些功能還是需要調用GDI API才能實現。我武斷的認為Image、Bitmap、Metafile跟HBITMAP對應,Graphics跟HDC對應。
在GDI+中,我們可以很方便的用Graphics.FromImage方法來操作Image中的內容,并將其保存回圖片文件。那么,我們怎么才能保存Graphics到圖片文件呢?創建一個Bitmap對象,復制Graphics g1的內容到Bitmap的Graphics g2,然后保存Bitmap對象到文件。復制過程我們必須通過PINVOKE調用BitBlt函數來實現。下面是該函數的聲明:
[DllImport("gdi32.dll", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
public static extern int BitBlt(HandleRef hDC, int x, int y, int nWidth, int nHeight, HandleRef hSrcDC, int xSrc, int ySrc, int dwRop);
參數中的各種DC可以用Graphics.GetHdc得到;最后一個參數光柵操作碼很多,截取屏幕用的SRCCOPY值是0xcc0020,完整的光柵操作碼可以查看MSDN的“Ternary Raster Operations”部分。
示例代碼如下:
//這里假設要保存一個窗體的內容
int width=800; //獲取寬度
int height=600; //獲取高度
const int SRCCOPY=0xcc0020; //復制圖塊的光柵操作碼
Bitmap bmSave=new Bitmap(width,height); //用于保存圖片的位圖對象
Graphics gSave=Graphics.FromImage(bmSave); //創建該位圖的Graphics對象
HandleRef hDcSave=new HandleRef(null,gSave.GetHdc()); //得到句柄
Graphics gSrc=formMain.CreateGraphics(); //創建窗體的Graphics對象
HandleRef hDcSrc=new HandleRef(null,gSrc.GetHdc());
BitBlt(hDcSave,0,0,width,height,hDcSrc,0,0,SRCCOPY);
gSrc.ReleaseHdc();
gSave.ReleaseHdc();
bmSave.Save(@"C:\test.bmp");
gSrc.Dispose();
gSave.Dispose();
bmSave.Dispose();
關于Graphics.CopyFromScreen方法
該方法在內部其實也使用BitBlt來進行圖塊復制,但是它只是固定的復制屏幕的內容,我們不能指定復制的源。
如果您需要截取屏幕的內容,可以使用以下代碼:
int screenWidth=System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏幕寬度
int screenHeight=System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度
Bitmap bmSave=new Bitmap(screenWidth,screenHeight);
Graphics g=Graphics.FromImage(bmSave);
g.CopyFromScreen(0,0,0,0,new Size(screenWidth,screenHeight),CopyPixelOperation.SourceCopy);
bmSave.Save(@"C:\test.bmp");
g.Dispose();
bmSave.Dispose();
如果需要復制頂層窗體的可見部分,也可以使用Graphics.CopyFromScreen,但是需要用PointToScreen方法先得到屏幕坐標。
如果需要復制任意的窗體或者控件,先用Control.Handle得到控件句柄,然后再用GetDC()函數得到HDC,再用BitBlt進行圖塊復制。