Advertisement
Guest User

Untitled

a guest
Jul 15th, 2010
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi,
  7.                   HBITMAP hBMP, HDC hDC);
  8. PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp);
  9.  
  10. void TakeScreenshot(char* Name)
  11. {
  12.     HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL);
  13.     HDC hdcCompatible = CreateCompatibleDC(hdcScreen);
  14.     HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen, GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES));
  15.  
  16.     if(hbmScreen == 0)
  17.         MessageBox(NULL, "hbmScreen", "Error", MB_OK);
  18.        
  19.     if(!SelectObject(hdcCompatible, hbmScreen))
  20.         MessageBox(NULL, "Compatible Bitmap Selection", "Error", MB_OK);
  21.  
  22.     if(!BitBlt(hdcCompatible, 0,0, GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES), hdcScreen, 0,0, SRCCOPY))
  23.         MessageBox(NULL, "Screen to Compat Blt Failed", "Error", MB_OK);
  24.  
  25.     PBITMAPINFO pBitmapInfo = CreateBitmapInfoStruct(hbmScreen);
  26.     CreateBMPFile(NULL, Name, pBitmapInfo, hbmScreen, hdcScreen);
  27. }
  28.  
  29. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  30. {
  31.    
  32.     return 0;
  33. }
  34.  
  35. PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp)
  36. {
  37.     BITMAP bmp;
  38.     PBITMAPINFO pbmi;
  39.     WORD    cClrBits;
  40.  
  41.     if(!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp))
  42.         MessageBox(NULL, "error", "Error", MB_OK);
  43.  
  44.     cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
  45.     if(cClrBits == 1)
  46.         cClrBits = 1;
  47.     else if(cClrBits <= 4)
  48.         cClrBits = 4;
  49.     else if(cClrBits <= 8)
  50.         cClrBits = 8;
  51.     else if(cClrBits <= 16)
  52.         cClrBits = 16;
  53.     else if(cClrBits <= 24)
  54.         cClrBits = 24;
  55.     else
  56.         cClrBits = 32;
  57.     if (cClrBits != 24)
  58.         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits));
  59.     else
  60.         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
  61.  
  62.     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  63.     pbmi->bmiHeader.biWidth = bmp.bmWidth;
  64.     pbmi->bmiHeader.biHeight = bmp.bmHeight;
  65.     pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
  66.     pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
  67.     if (cClrBits < 24)
  68.         pbmi->bmiHeader.biClrUsed = (1<<cClrBits);
  69.     pbmi->bmiHeader.biCompression = BI_RGB;
  70.     pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits + 31) & ~31) / 8 * pbmi->bmiHeader.biHeight;
  71.     pbmi->bmiHeader.biClrImportant = 0;
  72.     return pbmi;
  73.  }
  74.  
  75. void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC)
  76. {
  77.     HANDLE hf;              
  78.     BITMAPFILEHEADER hdr;    
  79.     PBITMAPINFOHEADER pbih;    
  80.     LPBYTE lpBits;      
  81.     DWORD dwTotal;              
  82.     DWORD cb;                  
  83.     BYTE *hp;
  84.     DWORD dwTmp;
  85.  
  86.     pbih = (PBITMAPINFOHEADER) pbi;
  87.     lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
  88.  
  89.     if(!lpBits)
  90.         MessageBox(NULL, "GlobalAlloc", "Error", MB_OK);
  91.     if(!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS))
  92.         MessageBox(NULL, "GetDIBits", "Error", MB_OK);
  93.  
  94.     hf = CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL);
  95.     if (hf == INVALID_HANDLE_VALUE)
  96.         MessageBox(NULL, "CreateFile", "Error", MB_OK);
  97.     hdr.bfType = 0x4d42;
  98.     hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
  99.     hdr.bfReserved1 = 0;
  100.     hdr.bfReserved2 = 0;
  101.     hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
  102.  
  103.     if(!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp,  NULL))
  104.         MessageBox(NULL, "error", "Error", MB_OK);
  105.     if(!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL))) MessageBox(NULL, "error", "Error", MB_OK);
  106.  
  107.     dwTotal = cb = pbih->biSizeImage;
  108.     hp = lpBits;
  109.     if(!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL))
  110.         MessageBox(NULL, "error", "Error", MB_OK);
  111.     if(!CloseHandle(hf))
  112.         MessageBox(NULL, "error", "Error", MB_OK);
  113.     GlobalFree((HGLOBAL)lpBits);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement