Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. static int s = 1;
  2. std::wostringstream oss;
  3. oss << std::setw(4) << std::setfill(L'0') << s++ << L".bmp";
  4. SaveBrowserImage(browser, oss.str(), buffer, width, height);
  5.  
  6. void SaveBrowserImage(CefRefPtr<CefBrowser> browser, const std::wstring &filename, const void* buffer, int width, int height)
  7.     {
  8.         // Populate the bitmap info header.
  9.         BITMAPINFOHEADER info;
  10.         info.biSize = sizeof(BITMAPINFOHEADER);
  11.         info.biWidth = width;
  12.         info.biHeight = -height;  // minus means top-down bitmap
  13.         info.biPlanes = 1;
  14.         info.biBitCount = 32;
  15.         info.biCompression = BI_RGB;  // no compression
  16.         info.biSizeImage = 0;
  17.         info.biXPelsPerMeter = 1;
  18.         info.biYPelsPerMeter = 1;
  19.         info.biClrUsed = 0;
  20.         info.biClrImportant = 0;
  21.  
  22.         // Create the bitmap and retrieve the bit buffer.
  23.         HDC screen_dc = GetDC(NULL);
  24.         void* bits = NULL;
  25.         HBITMAP bitmap = CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&info), DIB_RGB_COLORS, &bits, NULL, 0);
  26.         ReleaseDC(NULL, screen_dc);
  27.  
  28.         // Read the image into the bit buffer.
  29.         if (bitmap == NULL)
  30.             return;
  31.  
  32.         memcpy(bits, buffer, width * height * 4);
  33.  
  34.         // Populate the bitmap file header.
  35.         BITMAPFILEHEADER file;
  36.         file.bfType = 0x4d42;
  37.         file.bfSize = sizeof(BITMAPFILEHEADER);
  38.         file.bfReserved1 = 0;
  39.         file.bfReserved2 = 0;
  40.         file.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  41.  
  42.         // Write the bitmap to file.
  43.         HANDLE file_handle = CreateFile(filename.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  44.         if (file_handle != INVALID_HANDLE_VALUE)
  45.         {
  46.             DWORD bytes_written = 0;
  47.             WriteFile(file_handle, &file, sizeof(file), &bytes_written, 0);
  48.             WriteFile(file_handle, &info, sizeof(info), &bytes_written, 0);
  49.             WriteFile(file_handle, bits, width * height * 4, &bytes_written, 0);
  50.             CloseHandle(file_handle);
  51.         }
  52.  
  53.         DeleteObject(bitmap);
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement