首页 > 解决方案 > 仅从指定窗口捕获像素数据

问题描述

我希望下面的代码只截取指定窗口的屏幕截图,因为这样 BitBlt 更快。下面的代码截取由窗口名称指定的窗口的屏幕截图,将像素数据加载到缓冲区中,然后在屏幕上重新绘制图片,以证明复制有效。

我想截取 Google Chrome 等浏览器窗口的屏幕截图,但它似乎不起作用。它在我的屏幕上绘制了一个具有正确窗口尺寸的黑色矩形。

似乎一直使用 Minecraft 的窗口。

还与 Tor 浏览器一起工作。

我注意到在查询窗口信息后,我的世界的窗口没有child-windows,但是当它不能与其他窗口一起工作时,它们都有多个子窗口。

#include<iostream>
#include<Windows.h>
#include<vector>

using namespace std;

int main() {
    HWND wnd = FindWindow(NULL, "Minecraft 1.15.1");//Name of window to be screenshoted
    if (!wnd) {
        std::cout << "e1\n";
        std::cin.get();
        return 0;
    }
    WINDOWINFO wi = { 0 };
    wi.cbSize = sizeof(WINDOWINFO);

    GetWindowInfo(wnd, &wi);

    RECT rect;
    GetWindowRect(wnd, &rect);

    int width = wi.rcClient.right - wi.rcClient.left;
    int height = wi.rcClient.bottom - wi.rcClient.top;

    cout << width << height;
    /////Fetch window info^^^^^^^^



    BYTE* ScreenData = new BYTE[4 * width*height];


    // copy screen to bitmap
    HDC     hScreen = GetWindowDC(wnd);
    HDC     hDC = CreateCompatibleDC(hScreen);
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, width, height);
    HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
    BitBlt(hDC, 0, 0, width, height, hScreen, 0, 0, SRCCOPY);
    SelectObject(hDC, old_obj);

    BITMAPINFO bmi = { 0 };
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    std::cout << GetDIBits(hDC, hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS)<<endl;
    //std::cout << bmi.bmiHeader.biHeight<< " "<< bmi.bmiHeader.biWidth<<endl;
    BYTE*buffer = new BYTE[4* bmi.bmiHeader.biHeight*bmi.bmiHeader.biWidth];
    bmi.bmiHeader.biHeight *= -1;
    std::cout<<GetDIBits(hDC, hBitmap, 0, bmi.bmiHeader.biHeight, buffer, &bmi, DIB_RGB_COLORS)<<endl;//DIB_PAL_COLORS
    /////Load window pixels into a buffer^^^^^^^^


    POINT p;
    int r = 0;
    int g = 0;
    int b = 0;
    int x = 0;
    int y = 0;
    HDC sc = GetDC(NULL);
    COLORREF color;

    while (true) {
        if ((y*-1) == bmi.bmiHeader.biHeight+1 && x == bmi.bmiHeader.biWidth-1) { break; }
        r = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x)+2];
        g = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x)+1];
        b = (int)buffer[4 * ((y*bmi.bmiHeader.biWidth) + x) ];
        color = RGB(r, g, b);
        SetPixel(sc, x, y, color);

        x++;
        if (x == bmi.bmiHeader.biWidth) {
            y++;
            x = 0;
        }
    }
    /////Prove that the copying was successful and buffer is full^^^^^^^^
    Sleep(5000);
    std::cout << "fin\n";
    std::cin.get();
    return 0;
}

标签: c++bitmapgdiscreen-capturebitblt

解决方案


我认为问题在于你做事的顺序。

在将位图放到剪贴板上之前,您应该从内存设备上下文中选择它。

一旦您将位图放在剪贴板上,您就不再拥有它,因此您不应该尝试删除它。

最好的方法可能是将您的部分移到// clean up该部分之前// save bitmap to clipboard并删除您的DelectObject(hBitmap)陈述。所以你的代码的结尾可能应该是:

BitBlt(hDC, 0, 0, width, height, hScreen, 0, 0, SRCCOPY);

// clean up
SelectObject(hDC, old_obj); // selects hBitmap out of hDC
DeleteDC(hDC);
ReleaseDC(NULL, hScreen);

// save bitmap to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);  // clipboard now owns the bitmap
CloseClipboard();

如果在这些更改之后仍然有问题,我会检查 SetClipboardData 调用的返回值。如果失败,GetLastError 可能会提供线索。


推荐阅读