Windows下截圖

MagicProgram發表於2012-04-23
在Windows下對螢幕進行截圖。

原理:使用BitBlt函式進行截圖操作。

程式碼:
HWND hwnd = ::GetDesktopWindow();   
HDC hdc = GetWindowDC(NULL);   
int x = GetDeviceCaps(hdc, HORZRES);   
int y = GetDeviceCaps(hdc, VERTRES);   
HBITMAP hbmp = ::CreateCompatibleBitmap(hdc, x, y), hold; 
HDC hmemdc = ::CreateCompatibleDC(hdc);   
hold = (HBITMAP)::SelectObject(hmemdc,   hbmp);   
BitBlt(hmemdc, 0, 0, x, y, hdc, 0, 0, SRCCOPY); 
SelectObject(hmemdc, hold);   
Bitmap bit(hbmp, NULL);

分析:
通過以上程式碼,可以將桌面畫素都捕捉到Bitmap中,之後再對該物件進行操作。目前在PC機上測試(E5400 Dual-Core 2.7GHz),捕捉一張1024*768的桌面影像,平均需要47ms,捕捉一張800*600的桌面影像,平均需要16ms。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25432352/viewspace-722029/,如需轉載,請註明出處,否則將追究法律責任。

相關文章