Advertisement
Jong

Taking a Screenshot in C using Win32 API

Oct 28th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. //Taking screenshots in C using Win32 api.
  2. //Code sources:
  3. //http://codepads.blogspot.co.il/2011/06/how-to-save-hbitmap-handle-to-bmp-file.html
  4. //http://www.gamedev.net/topic/181269-win32-api-screenshot/
  5. //MSDN
  6.  
  7. #include <Windows.h>
  8. #include <stdio.h>
  9.  
  10. BOOL SaveToFile(HBITMAP hBitmap, LPCSTR lpszFileName)
  11. {
  12.   HDC hDC;
  13.  
  14.   int iBits;
  15.  
  16.   WORD wBitCount;
  17.  
  18.   DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0;
  19.  
  20.   BITMAP Bitmap;
  21.  
  22.   BITMAPFILEHEADER bmfHdr;
  23.  
  24.   BITMAPINFOHEADER bi;
  25.  
  26.   LPBITMAPINFOHEADER lpbi;
  27.  
  28.   HANDLE fh, hDib, hPal,hOldPal=NULL;
  29.  
  30.   hDC = CreateDCA("DISPLAY", NULL, NULL, NULL);
  31.   iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
  32.   DeleteDC(hDC);
  33.   if (iBits <= 1)
  34.     wBitCount = 1;
  35.   else if (iBits <= 4)
  36.     wBitCount = 4;
  37.   else if (iBits <= 8)
  38.     wBitCount = 8;
  39.   else
  40.     wBitCount = 24;
  41.   GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
  42.   bi.biSize = sizeof(BITMAPINFOHEADER);
  43.   bi.biWidth = Bitmap.bmWidth;
  44.   bi.biHeight = Bitmap.bmHeight;
  45.   bi.biPlanes = 1;
  46.   bi.biBitCount = wBitCount;
  47.   bi.biCompression = BI_RGB;
  48.   bi.biSizeImage = 0;
  49.   bi.biXPelsPerMeter = 0;
  50.   bi.biYPelsPerMeter = 0;
  51.   bi.biClrImportant = 0;
  52.   bi.biClrUsed = 0;
  53.   dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
  54.  
  55.   hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
  56.   lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
  57.   *lpbi = bi;
  58.  
  59.   hPal = GetStockObject(DEFAULT_PALETTE);
  60.   if (hPal)
  61.   {
  62.     hDC = GetDC(NULL);
  63.     hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
  64.     RealizePalette(hDC);
  65.   }
  66.  
  67.  
  68.   GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
  69.     +dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
  70.  
  71.   if (hOldPal)
  72.   {
  73.     SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
  74.     RealizePalette(hDC);
  75.     ReleaseDC(NULL, hDC);
  76.   }
  77.  
  78.   fh = CreateFileA(lpszFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
  79.     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  80.  
  81.   if (fh == INVALID_HANDLE_VALUE)
  82.     return FALSE;
  83.  
  84.   bmfHdr.bfType = 0x4D42; // "BM"
  85.   dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
  86.   bmfHdr.bfSize = dwDIBSize;
  87.   bmfHdr.bfReserved1 = 0;
  88.   bmfHdr.bfReserved2 = 0;
  89.   bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
  90.  
  91.   WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
  92.  
  93.   WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
  94.   GlobalUnlock(hDib);
  95.   GlobalFree(hDib);
  96.   CloseHandle(fh);
  97.   return TRUE;
  98. }
  99.  
  100. int main()
  101. {
  102.     HDC dcScreen, dcDest;
  103.     RECT screen;
  104.     HBITMAP screenshot;
  105.     BOOL res;
  106.  
  107.     printf("Press enter to take a screenshot!");
  108.     getchar();
  109.  
  110.     dcScreen = GetWindowDC(NULL); //Gets the DC for the entire screen;
  111.     GetWindowRect(GetDesktopWindow(), &screen);
  112.     dcDest = CreateCompatibleDC(dcScreen);
  113.     screenshot = CreateCompatibleBitmap(dcScreen, screen.right, screen.bottom);
  114.     SelectObject(dcDest, screenshot);
  115.     BitBlt(dcDest, 0, 0, screen.right, screen.bottom, dcScreen, 0, 0, SRCCOPY);
  116.     ReleaseDC(NULL, dcScreen);
  117.     DeleteDC(dcDest);
  118.  
  119.     res = SaveToFile(screenshot, "C:\\Users\\user\\Desktop\\screenshots\\screenshot.bmp");
  120.     printf("%d\n", res);
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement