D3D11 Screen Capture in C++

4.2k Views Asked by At

Is it possible to make a screen capture of a DirectX application (game for exemple) in D3D11 ?

I can do it easily in DirectX 9 or with API hooking (IDXGISwapChain::Present), but impossible with DirectX 11.

I initalize D3D11 and create the SwapChain with the Game window (FindWindow()), then I get the BackBuffer,

then ID3D11Texture2D::GetDesc() to use it in ID3D11Device::CreateTexture2D(), then ID3D11DeviceContext::CopyResource() which gives me a new ID3D11Texture2D resource.

When I save it with usual ways like D3DX11SaveTextureToFile(), I always get a Black image.

What did I forget ?

Thanks.

1

There are 1 best solutions below

0
AudioBubble On

I think here are two-step to check your code:

  1. ID3D11DeviceContext::CopyResource() does it work as expected? As we know, this func require strictly same desc of src and des, is src tex have some diff desc from des tex. you can output to a window directly to check before you save.

  2. D3DX11SaveTextureToFile is deprecated, which is replaced by SaveToXXXFile (where XXX is WIC, DDS, or TGA; WIC doesn't support DDS and TGA; D3DX 9 supported TGA as a common art source format for games). DirectXTex and DirectXTK library is new. But it still works now if you config appropriate. check this func return value. Make sure you do know how to use

    HRESULT D3DX11SaveTextureToFile(
           ID3D11DeviceContext      *pContext,
      _In_ ID3D11Resource           *pSrcTexture,
      _In_ D3DX11_IMAGE_FILE_FORMAT DestFormat,
      _In_ LPCTSTR                  pDestFile
    );