Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int s = 1;
- std::wostringstream oss;
- oss << std::setw(4) << std::setfill(L'0') << s++ << L".bmp";
- SaveBrowserImage(browser, oss.str(), buffer, width, height);
- void SaveBrowserImage(CefRefPtr<CefBrowser> browser, const std::wstring &filename, const void* buffer, int width, int height)
- {
- // Populate the bitmap info header.
- BITMAPINFOHEADER info;
- info.biSize = sizeof(BITMAPINFOHEADER);
- info.biWidth = width;
- info.biHeight = -height; // minus means top-down bitmap
- info.biPlanes = 1;
- info.biBitCount = 32;
- info.biCompression = BI_RGB; // no compression
- info.biSizeImage = 0;
- info.biXPelsPerMeter = 1;
- info.biYPelsPerMeter = 1;
- info.biClrUsed = 0;
- info.biClrImportant = 0;
- // Create the bitmap and retrieve the bit buffer.
- HDC screen_dc = GetDC(NULL);
- void* bits = NULL;
- HBITMAP bitmap = CreateDIBSection(screen_dc, reinterpret_cast<BITMAPINFO*>(&info), DIB_RGB_COLORS, &bits, NULL, 0);
- ReleaseDC(NULL, screen_dc);
- // Read the image into the bit buffer.
- if (bitmap == NULL)
- return;
- memcpy(bits, buffer, width * height * 4);
- // Populate the bitmap file header.
- BITMAPFILEHEADER file;
- file.bfType = 0x4d42;
- file.bfSize = sizeof(BITMAPFILEHEADER);
- file.bfReserved1 = 0;
- file.bfReserved2 = 0;
- file.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
- // Write the bitmap to file.
- HANDLE file_handle = CreateFile(filename.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (file_handle != INVALID_HANDLE_VALUE)
- {
- DWORD bytes_written = 0;
- WriteFile(file_handle, &file, sizeof(file), &bytes_written, 0);
- WriteFile(file_handle, &info, sizeof(info), &bytes_written, 0);
- WriteFile(file_handle, bits, width * height * 4, &bytes_written, 0);
- CloseHandle(file_handle);
- }
- DeleteObject(bitmap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement