How to get a screen image into a memory buffer?

后端 未结 2 476
孤独总比滥情好
孤独总比滥情好 2020-12-22 09:41

I\'ve been searching for a pure WIN32 way of getting the image data of a screen into a buffer, for analysis of the objects in the image later... Sadly I haven\'t found any.

2条回答
  •  借酒劲吻你
    2020-12-22 09:57

    There are multiple ways (you could use DirectX), but probably the simplest and easiest is to use GDI.

        // GetDC(0) will return screen device context
        HDC hScreenDC = GetDC(0);
        // Create compatible device context which will store the copied image
        HDC hMyDC= CreateCompatibleDC(hScreenDC );
    
        // Get screen properties
        int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
        int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
        int iBpi= GetDeviceCaps(hScreenDC ,BITSPIXEL);
    
        // Fill BITMAPINFO struct
        BITMAPINFO info;
        info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        info.bmiHeader.biWidth = iScreenWidth;
        info.bmiHeader.biHeight = iScreenHeight ;
        info.bmiHeader.biPlanes = 1;
        info.bmiHeader.biBitCount  = iBpi;
        info.bmiHeader.biCompression = BI_RGB;
    
        // Create bitmap, getting pointer to raw data (this is the important part)
        void *data;
        hBitmap = CreateDIBSection(hMyDC,&info,DIB_RGB_COLORS,(void**)&data,0,0);
        // Select it into your DC
        SelectObject(hMyDC,hBitmap);
    
        // Copy image from screen
        BitBlt(hMyDC, 0, 0, iScreenWidth, iScreenHeight, hScreenDC , 0, 0, SRCCOPY);
    
        // Remember to eventually free resources, etc.
    

    It's been a while since I did this so I may be forgetting something, but that's the gist of it. A word of warning: this is NOT fast; when the screen is moving a lot, it may take 50ms or more to capture one frame. It's basically doing the equivalent of pressing the PrintScreen key.

提交回复
热议问题