DirectX全屏应用程序捕获

时间:2018-05-09 14:50:17

标签: c++ directx screenshot

我遇到从全屏应用程序(游戏)捕获屏幕的问题。我根据这些链接中的代码制作了捕获屏幕的代码:

Capture screen using DirectX

DirectX Partial Screen Capture

但它仅适用于桌面上的某个地方,而不是全屏应用。试图将“parameters.Windowed”更改为false,不起作用,尝试在createdevice函数中使用某些选项,但是当全屏时它无法创建设备(保持null,hr返回的是0x88760868)。

代码:

void get_screen()
{
    UINT adapter = D3DADAPTER_DEFAULT;
    IDirect3D9 *d3d = nullptr;
    IDirect3DDevice9 *device = nullptr;
    IDirect3DSurface9 *surface = nullptr;
    D3DPRESENT_PARAMETERS parameters = { 0 };
    D3DDISPLAYMODE mode;  //screen resolution
    D3DLOCKED_RECT rc;
    UINT pitch;
    SYSTEMTIME st;
    LPBYTE shots = nullptr;
    HRESULT hr;

    // init D3D and get screen size
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    d3d->GetAdapterDisplayMode(adapter, &mode);

    parameters.Windowed = TRUE;
    parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
    parameters.BackBufferHeight = mode.Height;
    parameters.BackBufferWidth = mode.Width;
    parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;

    // create device & capture surface
    d3d->CreateDevice(adapter, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &parameters, &device);
    device->CreateOffscreenPlainSurface(mode.Width, mode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, nullptr);

    // get the data
    device->GetFrontBufferData(0, surface);
    surface->LockRect(&rc, NULL, 0);
    pitch = rc.Pitch;
    shots = new BYTE[pitch * mode.Height];
    CopyMemory(shots, rc.pBits, rc.Pitch * mode.Height);
    surface->UnlockRect();

    // save all screenshots
    WCHAR file[100] = L"test.png";
    save_screen(mode.Width, mode.Height, pitch, shots, file, GUID_ContainerFormatPng);

    //cleanup:
    surface->Release();
    device->Release();
    d3d->Release();
}

0 个答案:

没有答案