Last active
February 2, 2022 23:15
-
-
Save opsJson/a65f898304880876e2c1cdc506dc9156 to your computer and use it in GitHub Desktop.
Easily take screenshots on Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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