Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2010
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. #include <windows.h> // should be less than and greater than instead of \"
  2.  
  3. int CaptureBMP(LPCTSTR szFile)
  4. {
  5.     // Source[1]
  6.     HDC hdcScr, hdcMem;
  7.     HBITMAP hbmScr;
  8.     BITMAP bmp;
  9.     int iXRes, iYRes;
  10.  
  11.     // Create a normal DC and a memory DC for the entire screen. The
  12.     // normal DC provides a "snapshot" of the screen contents. The
  13.     // memory DC keeps a copy of this "snapshot" in the associated
  14.     // bitmap.
  15.     hdcScr = CreateDC("DISPLAY", NULL, NULL, NULL);
  16.     hdcMem = CreateCompatibleDC(hdcScr);
  17.  
  18.     iXRes = GetDeviceCaps(hdcScr, HORZRES);
  19.     iYRes = GetDeviceCaps(hdcScr, VERTRES);
  20.  
  21.     // Create a compatible bitmap for hdcScreen.
  22.     hbmScr = CreateCompatibleBitmap(hdcScr, iXRes, iYRes);
  23.     if (hbmScr == 0) return 0;
  24.  
  25.     // Select the bitmaps into the compatible DC.
  26.     if (!SelectObject(hdcMem, hbmScr)) return 0;
  27.  
  28.     // Copy color data for the entire display into a
  29.     // bitmap that is selected into a compatible DC.
  30.     if (!StretchBlt(hdcMem,
  31.         0, 0, iXRes, iYRes,
  32.         hdcScr,
  33.         0, 0, iXRes, iYRes,
  34.         SRCCOPY))
  35.  
  36.         return 0;
  37.  
  38.     // Source[2]
  39.     PBITMAPINFO pbmi;
  40.     WORD cClrBits;
  41.  
  42.     // Retrieve the bitmap's color format, width, and height.
  43.     if (!GetObject(hbmScr, sizeof(BITMAP), (LPSTR) &bmp)) return 0;
  44.  
  45.     // Convert the color format to a count of bits.
  46.     cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
  47.     if (cClrBits == 1)
  48.         cClrBits = 1;
  49.     else if (cClrBits <= 4)
  50.         cClrBits = 4;
  51.     else if (cClrBits <= 8)
  52.         cClrBits = 8;
  53.     else if (cClrBits <= 16)
  54.         cClrBits = 16;
  55.     else if (cClrBits <= 24)
  56.         cClrBits = 24;
  57.     else cClrBits = 32;
  58.  
  59.     // Allocate memory for the BITMAPINFO structure. (This structure
  60.     // contains a BITMAPINFOHEADER structure and an array of RGBQUAD
  61.     // data structures.)
  62.     if (cClrBits != 24)
  63.         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
  64.                 sizeof(BITMAPINFOHEADER) +
  65.                 sizeof(RGBQUAD) * (1 << cClrBits));
  66.  
  67.     // There is no RGBQUAD array for the 24-bit-per-pixel format.
  68.     else
  69.         pbmi = (PBITMAPINFO) LocalAlloc(LPTR,
  70.                 sizeof(BITMAPINFOHEADER));
  71.  
  72.     // Initialize the fields in the BITMAPINFO structure.
  73.     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  74.     pbmi->bmiHeader.biWidth = bmp.bmWidth;
  75.     pbmi->bmiHeader.biHeight = bmp.bmHeight;
  76.     pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
  77.     pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
  78.     if (cClrBits < 24)
  79.         pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
  80.  
  81.     // If the bitmap is not compressed, set the BI_RGB flag.
  82.     pbmi->bmiHeader.biCompression = BI_RGB;
  83.  
  84.     // Compute the number of bytes in the array of color
  85.     // indices and store the result in biSizeImage.
  86.     pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) / 8
  87.                                     * pbmi->bmiHeader.biHeight * cClrBits;
  88.  
  89.     // Set biClrImportant to 0, indicating that all of the
  90.     // device colors are important.
  91.     pbmi->bmiHeader.biClrImportant = 0;
  92.  
  93.     HANDLE hf;                  // file handle
  94.     BITMAPFILEHEADER hdr;       // bitmap file-header
  95.     PBITMAPINFOHEADER pbih;     // bitmap info-header
  96.     LPBYTE lpBits;              // memory pointer
  97.     DWORD dwTotal;              // total count of bytes
  98.     DWORD cb;                   // incremental count of bytes
  99.     BYTE *hp;                   // byte pointer
  100.     DWORD dwTmp;
  101.  
  102.     pbih = (PBITMAPINFOHEADER) pbmi;
  103.     lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
  104.  
  105.     if (!lpBits) return 0;
  106.  
  107.     // Retrieve the color table (RGBQUAD array) and the bits
  108.     // (array of palette indices) from the DIB.
  109.     if (!GetDIBits(hdcMem, hbmScr, 0, (WORD) pbih->biHeight, lpBits, pbmi, DIB_RGB_COLORS)) return 0;
  110.  
  111.     // Create the .BMP file.
  112.     hf = CreateFile(szFile,
  113.                     GENERIC_READ | GENERIC_WRITE,
  114.                     (DWORD) 0,
  115.                     NULL,
  116.                     CREATE_ALWAYS,
  117.                     FILE_ATTRIBUTE_NORMAL,
  118.                     (HANDLE) NULL);
  119.     if (hf == INVALID_HANDLE_VALUE) return 0;
  120.  
  121.     hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"
  122.  
  123.     // Compute the size of the entire file.
  124.     hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) +
  125.                  pbih->biSize + pbih->biClrUsed *
  126.                  sizeof(RGBQUAD) + pbih->biSizeImage);
  127.     hdr.bfReserved1 = 0;
  128.     hdr.bfReserved2 = 0;
  129.  
  130.     // Compute the offset to the array of color indices.
  131.     hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
  132.                     pbih->biSize + pbih->biClrUsed *
  133.                     sizeof (RGBQUAD);
  134.  
  135.     // Copy the BITMAPFILEHEADER into the .BMP file.
  136.     if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL)) return 0;
  137.  
  138.     // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.
  139.     if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER)
  140.                 + pbih->biClrUsed * sizeof (RGBQUAD),
  141.                 (LPDWORD) &dwTmp, NULL))
  142.         return 0;
  143.  
  144.     // Copy the array of color indices into the .BMP file.
  145.     dwTotal = cb = pbih->biSizeImage;
  146.     hp = lpBits;
  147.     if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp, NULL)) return 0;
  148.  
  149.     // Close the .BMP file.
  150.     if (!CloseHandle(hf)) return 0;
  151.  
  152.     // Free memory.
  153.     GlobalFree((HGLOBAL)lpBits);
  154.     ReleaseDC(0, hdcScr);
  155.     ReleaseDC(0, hdcMem);
  156.  
  157.     return 1;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement