Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Taking screenshots in C using Win32 api.
- //Code sources:
- //http://codepads.blogspot.co.il/2011/06/how-to-save-hbitmap-handle-to-bmp-file.html
- //http://www.gamedev.net/topic/181269-win32-api-screenshot/
- //MSDN
- #include <Windows.h>
- #include <stdio.h>
- BOOL SaveToFile(HBITMAP hBitmap, LPCSTR lpszFileName)
- {
- HDC hDC;
- int iBits;
- WORD wBitCount;
- DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0;
- BITMAP Bitmap;
- BITMAPFILEHEADER bmfHdr;
- BITMAPINFOHEADER bi;
- LPBITMAPINFOHEADER lpbi;
- HANDLE fh, hDib, hPal,hOldPal=NULL;
- hDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
- iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
- DeleteDC(hDC);
- if (iBits <= 1)
- wBitCount = 1;
- else if (iBits <= 4)
- wBitCount = 4;
- else if (iBits <= 8)
- wBitCount = 8;
- else
- wBitCount = 24;
- GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
- bi.biSize = sizeof(BITMAPINFOHEADER);
- bi.biWidth = Bitmap.bmWidth;
- bi.biHeight = Bitmap.bmHeight;
- bi.biPlanes = 1;
- bi.biBitCount = wBitCount;
- bi.biCompression = BI_RGB;
- bi.biSizeImage = 0;
- bi.biXPelsPerMeter = 0;
- bi.biYPelsPerMeter = 0;
- bi.biClrImportant = 0;
- bi.biClrUsed = 0;
- dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
- hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
- *lpbi = bi;
- hPal = GetStockObject(DEFAULT_PALETTE);
- if (hPal)
- {
- hDC = GetDC(NULL);
- hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
- RealizePalette(hDC);
- }
- GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
- +dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
- if (hOldPal)
- {
- SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
- RealizePalette(hDC);
- ReleaseDC(NULL, hDC);
- }
- fh = CreateFileA(lpszFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
- if (fh == INVALID_HANDLE_VALUE)
- return FALSE;
- bmfHdr.bfType = 0x4D42; // "BM"
- dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
- bmfHdr.bfSize = dwDIBSize;
- bmfHdr.bfReserved1 = 0;
- bmfHdr.bfReserved2 = 0;
- bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
- WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
- WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
- GlobalUnlock(hDib);
- GlobalFree(hDib);
- CloseHandle(fh);
- return TRUE;
- }
- int main()
- {
- HDC dcScreen, dcDest;
- RECT screen;
- HBITMAP screenshot;
- BOOL res;
- printf("Press enter to take a screenshot!");
- getchar();
- dcScreen = GetWindowDC(NULL); //Gets the DC for the entire screen;
- GetWindowRect(GetDesktopWindow(), &screen);
- dcDest = CreateCompatibleDC(dcScreen);
- screenshot = CreateCompatibleBitmap(dcScreen, screen.right, screen.bottom);
- SelectObject(dcDest, screenshot);
- BitBlt(dcDest, 0, 0, screen.right, screen.bottom, dcScreen, 0, 0, SRCCOPY);
- ReleaseDC(NULL, dcScreen);
- DeleteDC(dcDest);
- res = SaveToFile(screenshot, "C:\\Users\\user\\Desktop\\screenshots\\screenshot.bmp");
- printf("%d\n", res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement