Skip to content

Instantly share code, notes, and snippets.

@equalent
Forked from holmesconan/capture.cpp
Created August 18, 2019 16:41
Show Gist options
  • Save equalent/3901276be3704165ad8ed2673e19d5e8 to your computer and use it in GitHub Desktop.
Save equalent/3901276be3704165ad8ed2673e19d5e8 to your computer and use it in GitHub Desktop.
Capture screen by GDI on Windows
HDC hdc = GetDC(NULL); // get the desktop device context
HDC hDest = CreateCompatibleDC(hdc); // create a device context to use yourself
// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hdc, width, height);
// use the previously created device context with the bitmap
SelectObject(hDest, hbDesktop);
// copy from the desktop device context to the bitmap device context
// call this once per 'frame'
BitBlt(hDest, 0,0, width, height, hdc, 0, 0, SRCCOPY);
// after the recording is done, release the desktop context you got..
ReleaseDC(NULL, hdc);
// ..and delete the context you created
DeleteDC(hDest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment