Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HBITMAP CaptureAnImage(HWND hWnd, UINT nWidth, UINT nHeight)
- {
- HDC hdcScreen;
- HDC hdcWindow;
- HDC hdcMemDC = NULL;
- HBITMAP hbmScreen = NULL;
- BITMAP bmpScreen;
- // Retrieve the handle to a display device context for the client
- // area of the window.
- hdcScreen = GetDC(NULL);
- hdcWindow = GetDC(hWnd);
- // Create a compatible DC which is used in a BitBlt from the window DC
- hdcMemDC = CreateCompatibleDC(hdcWindow);
- if (!hdcMemDC)
- {
- MessageBox(hWnd, L"CreateCompatibleDC has failed", L"Failed", MB_OK);
- goto done;
- }
- //This is the best stretch mode
- SetStretchBltMode(hdcWindow, HALFTONE);
- //The source DC is the entire screen and the destination DC is the current window (HWND)
- if (!StretchBlt(hdcWindow,
- 0, 0,
- nWidth, nHeight,
- hdcScreen,
- 0, 0,
- GetSystemMetrics(SM_CXSCREEN),
- GetSystemMetrics(SM_CYSCREEN),
- SRCCOPY))
- {
- MessageBox(hWnd, L"StretchBlt has failed", L"Failed", MB_OK);
- goto done;
- }
- // Create a compatible bitmap from the Window DC
- hbmScreen = CreateCompatibleBitmap(hdcWindow, nWidth, nHeight);
- if (!hbmScreen)
- {
- MessageBox(hWnd, L"CreateCompatibleBitmap Failed", L"Failed", MB_OK);
- goto done;
- }
- // Select the compatible bitmap into the compatible memory DC.
- SelectObject(hdcMemDC, hbmScreen);
- // Bit block transfer into our compatible memory DC.
- if (!BitBlt(hdcMemDC,
- 0, 0,
- nWidth, nHeight,
- hdcWindow,
- 0, 0,
- SRCCOPY))
- {
- MessageBox(hWnd, L"BitBlt has failed", L"Failed", MB_OK);
- goto done;
- }
- // Get the BITMAP from the HBITMAP
- GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
- BITMAPFILEHEADER bmfHeader;
- BITMAPINFOHEADER bi;
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = bmpScreen.bmWidth;
- bi.biHeight = bmpScreen.bmHeight;
- bi.biPlanes = 1;
- bi.biBitCount = 32;
- bi.biCompression = BI_RGB;
- bi.biSizeImage = 0;
- bi.biXPelsPerMeter = 0;
- bi.biYPelsPerMeter = 0;
- bi.biClrUsed = 0;
- bi.biClrImportant = 0;
- DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
- // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
- // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
- // have greater overhead than HeapAlloc.
- HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
- char *lpbitmap = (char *)GlobalLock(hDIB);
- // Gets the "bits" from the bitmap and copies them into a buffer
- // which is pointed to by lpbitmap.
- GetDIBits(hdcWindow, hbmScreen, 0,
- (UINT)bmpScreen.bmHeight,
- lpbitmap,
- (BITMAPINFO *)&bi, DIB_RGB_COLORS);
- // A file is created, this is where we will save the screen capture.
- HANDLE hFile = CreateFile(L"captureqwsx.bmp",
- GENERIC_WRITE,
- 0,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL);
- // Add the size of the headers to the size of the bitmap to get the total file size
- DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- //Offset to where the actual bitmap bits start.
- bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
- //Size of the file
- bmfHeader.bfSize = dwSizeofDIB;
- //bfType must always be BM for Bitmaps
- bmfHeader.bfType = 0x4D42; //BM
- DWORD dwBytesWritten = 0;
- WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
- WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
- WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
- //Unlock and Free the DIB from the heap
- GlobalUnlock(hDIB);
- GlobalFree(hDIB);
- //Close the handle for the file that was created
- CloseHandle(hFile);
- //Clean up
- done:
- //DeleteObject(hbmScreen);
- DeleteObject(hdcMemDC);
- ReleaseDC(NULL, hdcScreen);
- ReleaseDC(hWnd, hdcWindow);
- return hbmScreen;
- }
- HBITMAP GetWndSnapshot(HWND hWnd)
- {
- if (!hWnd) return NULL;
- RECT rect;
- ::GetWindowRect(hWnd, &rect);
- int nWidth = rect.right - rect.left;
- int nHeight = rect.bottom - rect.top;
- HDC hDC = ::GetWindowDC(hWnd);
- HDC memDC = ::CreateCompatibleDC(hDC);
- HBITMAP hBitmap = ::CreateCompatibleBitmap(hDC, nWidth, nHeight);
- HBITMAP oldBitmap = (HBITMAP)::SelectObject(memDC, hBitmap);
- ::BitBlt(memDC, 0, 0, nWidth, nHeight, hDC, 0, 0, SRCCOPY);
- ::SelectObject(memDC, oldBitmap);
- ::DeleteDC(memDC);
- ::ReleaseDC(hWnd, hDC);
- return hBitmap;
- }
- HBITMAP Create32BppDIBSection(int nWidth, int nHeight)
- {
- BITMAPINFO bmi;
- memset(&bmi, 0, sizeof(bmi));
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = nWidth;
- bmi.bmiHeader.biHeight = -nHeight;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- LPVOID pvBits = NULL;
- return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, &pvBits, NULL, 0);
- }
- HBITMAP ConvertBitmap24To32(HBITMAP hbmp24)
- {
- if (!hbmp24) return NULL;
- BITMAP bm24;
- if (!GetObject(hbmp24, sizeof(BITMAP), &bm24)) return NULL;
- if (bm24.bmBitsPixel != 24) return NULL;
- HBITMAP hbmp32 = Create32BppDIBSection(bm24.bmWidth, bm24.bmHeight);
- if (!hbmp32) return NULL;
- BITMAP bm32;
- if (!GetObject(hbmp32, sizeof(BITMAP), &bm32)) return NULL;
- for (int i = 0; i < bm32.bmHeight; ++i)
- {
- RGBQUAD *pRowDst = (RGBQUAD *)((LPBYTE)bm32.bmBits + (bm32.bmHeight - 1 - i) * bm32.bmWidthBytes);
- RGBTRIPLE *pRowSrc = (RGBTRIPLE *)((LPBYTE)bm24.bmBits + i * bm24.bmWidthBytes);
- for (int j = 0; j < bm32.bmWidth; ++j)
- {
- pRowDst[j].rgbBlue = pRowSrc[j].rgbtBlue;
- pRowDst[j].rgbGreen = pRowSrc[j].rgbtGreen;
- pRowDst[j].rgbRed = pRowSrc[j].rgbtRed;
- }
- }
- return hbmp32;
- }
Add Comment
Please, Sign In to add comment