捕获任何Windows应用程序的屏幕?

时间:2017-10-09 05:27:43

标签: c++ windows capture direct3d screen-capture

我正在尝试编写一个Windows C ++程序,该程序将尝试从屏幕上当前显示的内容中选择一种感兴趣的颜色。

我尝试过以下关于GDI,Direct3D9和Direct3D11 DXGI的示例,他们所有似乎仅用于捕获Windows桌面和/或我自己的应用程序自己的输出。当我推出全屏Direct3D游戏时,我似乎最终得到了一些空白像素数据。

必须才能实现此目的,否则OBS Studio,FRAPS等将无法像它们那样透明地工作。

我知道我可以尝试对OBS Studio进行逆向工程,但是有没有人有更简洁的C ++解决方案来捕获任意Windows应用程序的视频输出作为某种像素缓冲区?

编辑:我还应该提一下,捕获常规桌面窗口似乎有效。这是给我带来麻烦的全屏游戏。

编辑:评论者要求我的GDI代码。这是我的GDI和D3D9代码。正如您所看到的,我根据我发现的相互矛盾的示例尝试了一些变体:

std::wstring GetScreenColor(COLORREF& colorRef)
{
    std::wstring retVal;

    //const int desktopWidth(GetDeviceCaps(desktopHdc, HORZRES));
    //const int desktopHeight(GetDeviceCaps(desktopHdc, VERTRES));
//        const int desktopWidth(GetSystemMetrics(SM_CXVIRTUALSCREEN));
//        const int desktopHeight(GetSystemMetrics(SM_CYVIRTUALSCREEN));
    const int desktopWidth(GetSystemMetrics(SM_CXSCREEN));
    const int desktopHeight(GetSystemMetrics(SM_CYSCREEN));
    HWND desktopHwnd(GetDesktopWindow());
//        HDC desktopHdc(GetDC(NULL));
    HDC desktopHdc(GetDC(desktopHwnd));
    HDC myHdc(CreateCompatibleDC(desktopHdc));
    const HBITMAP desktopBitmap(CreateCompatibleBitmap(desktopHdc, desktopWidth, desktopHeight));
    SelectObject(myHdc, desktopBitmap);
//        BitBlt(myHdc, 0, 0, desktopWidth, desktopHeight, desktopHdc, 0, 0, SRCCOPY);
    BitBlt(myHdc, 0, 0, desktopWidth, desktopHeight, desktopHdc, 0, 0, SRCCOPY | CAPTUREBLT);
    //SelectObject(myHdc, hOld);
    ReleaseDC(NULL, desktopHdc);

    BITMAPINFO bitmapInfo = { 0 };
    bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
    bitmapInfo.bmiHeader.biWidth = desktopWidth;
    bitmapInfo.bmiHeader.biHeight = -desktopHeight;
    bitmapInfo.bmiHeader.biPlanes = 1;
    bitmapInfo.bmiHeader.biBitCount = 24;
    bitmapInfo.bmiHeader.biCompression = BI_RGB;
    bitmapInfo.bmiHeader.biSizeImage = 0;

    // TODO: use a persistent buffer?
    const unsigned long numPixels(desktopHeight * desktopWidth);
    ColorBGRS* rawPixels(new ColorBGRS[numPixels]);
    if (!GetDIBits(myHdc, desktopBitmap, 0, desktopHeight, rawPixels, &bitmapInfo, DIB_RGB_COLORS))
    {
        delete[] rawPixels;
        ReleaseDC(desktopHwnd, desktopHdc);
        DeleteDC(myHdc);
        DeleteObject(desktopBitmap);
        return L"GetDIBits() failed";
    }

    unsigned long redSum(0);
    unsigned long greenSum(0);
    unsigned long blueSum(0);

    for (unsigned long index(0); index < numPixels; ++index)
    {
        blueSum  += rawPixels[index].blue;
        greenSum += rawPixels[index].green;
        redSum   += rawPixels[index].red;
    }

    const unsigned long redAverage(redSum / numPixels);
    const unsigned long blueAverage(blueSum / numPixels);
    const unsigned long greenAverage(greenSum / numPixels);

    colorRef = RGB(redAverage, greenAverage, blueAverage);

    delete[] rawPixels;
    ReleaseDC(desktopHwnd, desktopHdc);
    DeleteDC(myHdc);
    DeleteObject(desktopBitmap);

    return std::wstring();
}

std::wstring GetScreenColor2(COLORREF& colorRef)
{
    IDirect3D9* d3d9(Direct3DCreate9(D3D_SDK_VERSION));
    if (!d3d9)
    {
        d3d9->Release();
        return L"Direct3DCreate9() failed";
    }

    D3DDISPLAYMODE d3dDisplayMode;
    if (FAILED(d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3dDisplayMode)))
    {
        return L"GetAdapterDisplayMode() failed";
    }

    D3DPRESENT_PARAMETERS d3dPresentParams;
    ZeroMemory(&d3dPresentParams, sizeof(D3DPRESENT_PARAMETERS));        
    d3dPresentParams.Windowed = TRUE;
    d3dPresentParams.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
    d3dPresentParams.BackBufferFormat = d3dDisplayMode.Format;
    d3dPresentParams.BackBufferCount = 1;
    d3dPresentParams.BackBufferHeight = d3dDisplayMode.Height;
    d3dPresentParams.BackBufferWidth = d3dDisplayMode.Width;
    d3dPresentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    //d3dPresentParams.SwapEffect = D3DSWAPEFFECT_COPY;
    d3dPresentParams.hDeviceWindow = NULL; //hWnd;
    d3dPresentParams.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
    d3dPresentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

    IDirect3DDevice9* d3d9Device(0);
    if (FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dPresentParams.hDeviceWindow, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPresentParams, &d3d9Device)))
    {
        d3d9->Release();
        return L"CreateDevice() failed";
    }

    IDirect3DSurface9* d3d9Surface(0);
//        if (FAILED(d3d9Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &d3d9Surface))) return L"GetBackBuffer() failed";
    if (FAILED(d3d9Device->CreateOffscreenPlainSurface(d3dDisplayMode.Width, d3dDisplayMode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &d3d9Surface, NULL)))
//        if (FAILED(d3d9Device->CreateOffscreenPlainSurface(d3dDisplayMode.Width, d3dDisplayMode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &d3d9Surface, NULL)))
    {
        d3d9Device->Release();
        d3d9->Release();
        return L"CreateOffscreenPlainSurface() failed";
    }

    if (FAILED(d3d9Device->GetFrontBufferData(0, d3d9Surface)))
    {
        d3d9Surface->Release();
        d3d9Device->Release();
        d3d9->Release();
        return L"GetFrontBufferData() failed";
    }

    D3DLOCKED_RECT d3dLockedRect;
    if (FAILED(d3d9Surface->LockRect(&d3dLockedRect, 0, D3DLOCK_NO_DIRTY_UPDATE |
                                                        D3DLOCK_NOSYSLOCK |
                                                        D3DLOCK_READONLY)))
    {
        d3d9Surface->UnlockRect();
        d3d9Surface->Release();
        d3d9Device->Release();
        d3d9->Release();
        return L"LockRect() failed";
    }

    const unsigned long numPixels(d3dDisplayMode.Height * d3dDisplayMode.Width);
    BYTE* rawPixels((BYTE*)(d3dLockedRect.pBits));
    colorRef = RGB(*(rawPixels + 2), *(rawPixels + 1), *(rawPixels));

    d3d9Surface->UnlockRect();
    d3d9Surface->Release();
    d3d9Device->Release();
    d3d9->Release();

    return std::wstring();
}

1 个答案:

答案 0 :(得分:2)

自Windows 8以来,Desktop Duplication API能够记录游戏等全屏应用程序。我最近为我的一个项目做了this library,你可以用它作为参考。只有在你的情况下,你需要从纹理中获取原始像素数据,而不是将它们写入视频或图像。

编辑:添加了一个关于丢失访问的重新初始化的小例子。

{{ $test ? 'true' : 'false' }}