抓屏樣本

來源:互聯網
上載者:User
以下樣本程式功能為:抓取整個螢幕並儲存為D:\\1234.bmp
#include <windows.h>#include <iostream>using namespace std;//// 抓取螢幕映像存入DDB位元影像//HBITMAP CaptureScreenToDDB(){HDC hdcScreen = NULL;HDC hdcMem = NULL;HBITMAP hBitmap = NULL;HBITMAP hOldBitmap = NULL;int xScreenRes = 0;int yScreenRes = 0;// 建立螢幕裝置描述表hdcScreen = ::CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);// 擷取螢幕解析度xScreenRes = ::GetDeviceCaps(hdcScreen, HORZRES);yScreenRes = ::GetDeviceCaps(hdcScreen, VERTRES);// 建立位元影像並拷貝螢幕至位元影像hdcMem = ::CreateCompatibleDC(hdcScreen);hBitmap = ::CreateCompatibleBitmap(hdcScreen, xScreenRes, yScreenRes);hOldBitmap = (HBITMAP)::SelectObject(hdcMem, hBitmap);::BitBlt(hdcMem, 0, 0, xScreenRes, yScreenRes, hdcScreen, 0, 0, SRCCOPY);hBitmap = (HBITMAP)::SelectObject(hdcMem, hOldBitmap);// 釋放資源::DeleteDC(hdcMem);::DeleteDC(hdcScreen);return hBitmap;}//// 儲存DDB位元影像成本地DIB位元影像檔案//int SaveDDBToFile(HBITMAP hBitmap, HDC hdc, TCHAR name[]){// 擷取位元影像資訊BITMAP bm;::GetObject(hBitmap, sizeof(bm), &bm);// 計算位元影像檔案每個位元組所佔的bit數int iBitsPixel = bm.bmBitsPixel * bm.bmPlanes;if (iBitsPixel < 24)return -1;// 位元影像資訊頭結構 BITMAPINFOHEADER bmih = { 0 };bmih.biSize = sizeof(bmih);bmih.biWidth = bm.bmWidth;bmih.biHeight = bm.bmHeight;bmih.biPlanes = 1;bmih.biBitCount = iBitsPixel;bmih.biCompression = BI_RGB;bmih.biSizeImage = 0;bmih.biXPelsPerMeter = 0;bmih.biYPelsPerMeter = 0;bmih.biClrUsed = 0;bmih.biClrImportant = 0;// 調色盤DWORD dwPaletteSize = 0;// 建立DIB位元影像內容DWORD dwSize = ((bm.bmWidth * iBitsPixel + 31) / 32) * 4 * bm.bmHeight;DWORD dwDIBSize = sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwSize;LPVOID lpDIB = ::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, dwDIBSize);LPBITMAPINFOHEADER lpbmih = (LPBITMAPINFOHEADER)lpDIB;*lpbmih = bmih;::GetDIBits(hdc, hBitmap, 0, bm.bmHeight, (char*)lpDIB + sizeof(BITMAPINFOHEADER) + dwPaletteSize,(PBITMAPINFO)lpbmih, DIB_RGB_COLORS);// 位元影像檔案頭BITMAPFILEHEADER bmfh;bmfh.bfType = 0x4d42;bmfh.bfSize = sizeof(BITMAPFILEHEADER) + dwDIBSize;bmfh.bfReserved1 = 0;bmfh.bfReserved2 = 0;bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize;// 建立.bmp檔案並儲存HANDLE hFile = ::CreateFile(name,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if (INVALID_HANDLE_VALUE == hFile)return GetLastError();DWORD dwWritten = 0;::WriteFile(hFile, &bmfh, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);::WriteFile(hFile, lpDIB, dwDIBSize, &dwWritten, NULL);::HeapFree(::GetProcessHeap(), 0, lpDIB);::CloseHandle(hFile);return 0;}int main(){HDC hdcScreen = ::CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);HBITMAP hBitmapScreen = CaptureScreenToDDB();if (hBitmapScreen == NULL){cout << "CaptureScreenToDDB() failed." << endl;return -1;}TCHAR name[] = TEXT("d:\\1234.bmp");cout << SaveDDBToFile(hBitmapScreen, hdcScreen, name) << endl;cout << "Hello Kitty" << endl;system("pause");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.