1

I have a problem with capturing screen from full screen application (game). I made code to capture screen based on code from those links:

Capture screen using DirectX

DirectX Partial Screen Capture

But it only works when using somewhere on desktop, not in full screen app. Tried to change "parameters.Windowed" to false, not worked, tried some options within createdevice function, but when full screen it cannot create device (stays null, hr returned is 0x88760868).

Code:

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();
}
  • Robust capture of fullscreen rendering on modern versions of Windows is quite challenging, and won't work with legacy Direct3D 9 APIs anyhow since DXGI handles the swapchain. Have you considered using something like Windows 10 Gamebar or [FRAPS](http://fraps.com/) instead? – Chuck Walbourn May 09 '18 at 17:04
  • I don't need to record gameplay, so FRAPS or gamebar isn't exactly what I look for. I need something to process get screen for further processing inside aplication. I hoped to make simple bot template. Something that could be easily adapted to any game, to skip most boring grinders (eg. infamous 1000 iron daggers crafting). I don't have to be necessary DX9, but that would work with lots of games without messing with dll, also easy to restore for edits after reinstalling OS. – user2420655 May 11 '18 at 22:14
  • If you are just wanting to capture a render target from your own program, use ``ScreenShot`` in the [DirectX Tool Kit](https://github.com/Microsoft/DirectXTK/wiki/ScreenGrab). – Chuck Walbourn May 11 '18 at 23:56

0 Answers0