Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active February 2, 2022 23:15
Show Gist options
  • Select an option

  • Save opsJson/a65f898304880876e2c1cdc506dc9156 to your computer and use it in GitHub Desktop.

Select an option

Save opsJson/a65f898304880876e2c1cdc506dc9156 to your computer and use it in GitHub Desktop.
Easily take screenshots on Windows.
#include <windows.h>
int* screenshot(int x, int y, int width, int height) {
HDC screen = GetDC(0);
HDC memoryDC = CreateCompatibleDC(screen);
HBITMAP memoryBMP = CreateCompatibleBitmap(screen, width, height);
SelectObject(memoryDC, memoryBMP);
BITMAPINFO bitmapinfo;
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth = width;
bitmapinfo.bmiHeader.biHeight = height;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 32;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
int *pixels = (int*)malloc(sizeof(int) * width * height);
BitBlt(memoryDC, 0, 0, width, height, screen, x, y, SRCCOPY);
GetDIBits(memoryDC, memoryBMP, 0, height, pixels, &bitmapinfo, DIB_RGB_COLORS);
DeleteObject(memoryBMP);
DeleteDC(memoryDC);
return pixels;
}
/*///////////////////////////////////
Testing:
///////////////////////////////////*/
int main() {
int *image = screenshot(0, 0, 500, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment