capturing a screen image under Windows can have multiple methods, and it's easier to call a third-party library, such as the Qt screenshot API.
Here's how to use the Windows API to implement screenshots and convert to RGB format storage.
#include <windows.h>//final F's memory layout is Bgra format, need to ensure that the BUF length is sufficient (>w*h*4) void ScreenCap (void* buf, int* W, int* h) {HWND hdesk = GetDesktopWindow (); HDC Hscreen = GetDC (hdesk); int width = GetDeviceCaps (Hscreen, horzres); int height = GetDeviceCaps (hscreen, vertres); if (w! = 0) *w = width; if (h! = 0) *h = height; HDC Hdcmem = CreateCompatibleDC (Hscreen); Hbitmap hbitmap = CreateCompatibleBitmap (hscreen, width, height); Bitmapinfoheader BMI = {0}; bmi.bisize = sizeof (Bitmapinfoheader); Bmi.biplanes = 1; Bmi.bibitcount = 32; Bmi.biwidth = width; Bmi.biheight =-height; Bmi.bicompression = Bi_rgb; Bmi.bisizeimage = Width*height; SelectObject (Hdcmem, HBITMAP); BitBlt (hdcmem, 0, 0, width, height, hscreen, 0, 0, srccopy); GetDIBits (Hdcmem, hbitmap, 0, height, buf, (bitmapinfo*) &bmi, dib_rgb_colors); DeleteDC (HDCMEM); ReleaseDC (Hdesk, Hscreen); CloseWindow (hdesk); DeleteObject (HBITMAP);}
Use WindowsAPI screenshot and convert to RGB format