Capturing a Screenshot Using Win32
Question:
How do I capture a screenshot of the current screen in a Windows application using Win32?
Answer:
To capture a screenshot using Win32, follow these steps:
- Obtain a device context for the screen using GetDC(nullptr).
- Create a compatible device context for holding the captured image using CreateCompatibleDC(hScreenDC).
- Get the screen resolution using GetDeviceCaps(hScreenDC, HORZRES) for width and GetDeviceCaps(hScreenDC, VERTRES) for height.
- Create a compatible bitmap with the screen resolution using CreateCompatibleBitmap(hScreenDC, width, height).
- Select the bitmap into the compatible device context using SelectObject(hMemoryDC, hBitmap).
- Copy the screen contents into the bitmap using BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY).
- Copy the bitmap back to the device context using SelectObject(hMemoryDC, hOldBitmap).
- Delete the compatible device context using DeleteDC(hMemoryDC).
- Delete the screen device context using DeleteDC(hScreenDC).
The above is the detailed content of How to Capture a Screenshot in Windows Using Win32 API?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn