Skip to content

Instantly share code, notes, and snippets.

@davidglezz
Created November 4, 2013 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidglezz/7305604 to your computer and use it in GitHub Desktop.
Save davidglezz/7305604 to your computer and use it in GitHub Desktop.
make a screenshot and save it to disk. Windows
#include <windows.h>
int main()
{
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int size = width * height * 3;
int headerSize = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
BITMAPFILEHEADER bmFile = {0x4D42, headerSize + size, 0, 0, headerSize};
BITMAPINFO bmInfo = {{sizeof(BITMAPINFOHEADER), width, height, 1, 24, BI_RGB, size, 0, 0, 0, 0}};
LPBYTE pixels;
HDC hdc = CreateCompatibleDC(0);
HBITMAP hBMP = CreateDIBSection(hdc, &bmInfo, DIB_RGB_COLORS, (LPVOID*)&pixels, 0, 0);
SelectObject(hdc, hBMP);
BitBlt(hdc, 0, 0, width, height, GetDC(0), left, top, SRCCOPY);
DeleteDC(hdc);
HANDLE hFile = CreateFile("c:\\Screenshot.bmp", FILE_WRITE_DATA, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD dwOut;
WriteFile(hFile,&bmFile, sizeof(BITMAPFILEHEADER), &dwOut, NULL);
WriteFile(hFile,&bmInfo, sizeof(BITMAPINFOHEADER), &dwOut, NULL);
WriteFile(hFile, pixels, size, &dwOut, NULL);
CloseHandle(hFile);
}
DeleteObject(hBMP);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment