最近在嘗試為軟件增加截取屏幕的功能,為此學(xué)習(xí)了System.Drawing命名空間的Graphics、Image、Bitmap等GDI+類,這些類都很方便使用。但是它們大多都是對(duì)原有GDI API的封裝,也增加了一些新的東西;不過(guò)封裝得并不徹底,有些功能還是需要調(diào)用GDI API才能實(shí)現(xiàn)。我武斷的認(rèn)為Image、Bitmap、Metafile跟HBITMAP對(duì)應(yīng),Graphics跟HDC對(duì)應(yīng)。
在GDI+中,我們可以很方便的用Graphics.FromImage方法來(lái)操作Image中的內(nèi)容,并將其保存回圖片文件。那么,我們?cè)趺床拍鼙4鍳raphics到圖片文件呢?創(chuàng)建一個(gè)Bitmap對(duì)象,復(fù)制Graphics g1的內(nèi)容到Bitmap的Graphics g2,然后保存Bitmap對(duì)象到文件。復(fù)制過(guò)程我們必須通過(guò)PINVOKE調(diào)用BitBlt函數(shù)來(lái)實(shí)現(xiàn)。下面是該函數(shù)的聲明:
[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);
參數(shù)中的各種DC可以用Graphics.GetHdc得到;最后一個(gè)參數(shù)光柵操作碼很多,截取屏幕用的SRCCOPY值是0xcc0020,完整的光柵操作碼可以查看MSDN的“Ternary Raster Operations”部分。
示例代碼如下:
//這里假設(shè)要保存一個(gè)窗體的內(nèi)容
int width=800; //獲取寬度
int height=600; //獲取高度
const int SRCCOPY=0xcc0020; //復(fù)制圖塊的光柵操作碼
Bitmap bmSave=new Bitmap(width,height); //用于保存圖片的位圖對(duì)象
Graphics gSave=Graphics.FromImage(bmSave); //創(chuàng)建該位圖的Graphics對(duì)象
HandleRef hDcSave=new HandleRef(null,gSave.GetHdc()); //得到句柄
Graphics gSrc=formMain.CreateGraphics(); //創(chuàng)建窗體的Graphics對(duì)象
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();
關(guān)于Graphics.CopyFromScreen方法
該方法在內(nèi)部其實(shí)也使用BitBlt來(lái)進(jìn)行圖塊復(fù)制,但是它只是固定的復(fù)制屏幕的內(nèi)容,我們不能指定復(fù)制的源。
如果您需要截取屏幕的內(nèi)容,可以使用以下代碼:
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();
如果需要復(fù)制頂層窗體的可見部分,也可以使用Graphics.CopyFromScreen,但是需要用PointToScreen方法先得到屏幕坐標(biāo)。
如果需要復(fù)制任意的窗體或者控件,先用Control.Handle得到控件句柄,然后再用GetDC()函數(shù)得到HDC,再用BitBlt進(jìn)行圖塊復(fù)制。