1

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
    It is possible, but not trivial since you'll have to do the API hooking you mention. OBS project probably does it "somewhere in its code" see also http://stackoverflow.com/questions/5069104/fastest-method-of-screen-capturing – rogerdpack Mar 25 '16 at 17:10
  • If you target Windows 8 clients, Desktop Duplication API is the way to go: https://msdn.microsoft.com/library/windows/desktop/hh404487.aspx – Simon Mourier Mar 25 '16 at 17:13
  • OBS uses the hook I quoted (DXGICapture.cpp file). It is easy like with DirectX9, but I don't understand why the similar method with DirectX11 gives a black image – Julien Garrigues Mar 25 '16 at 17:54
  • It is black probably because the CopyResource fail, you can try to force on the debug layer with the dx control panel, and get more information of why it fails – galop1n Mar 25 '16 at 23:56
  • Take a look at the implementation of [ScreenGrab](https://github.com/Microsoft/DirectXTK/blob/master/Src/ScreenGrab.cpp) as you may just be missing a resolve step. That said, keep in mind that API hooking looks a lot like malware to A/V software. Note also that D3DX11 is [deprecated](https://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx) and has a number of bugs that will never be fixed. – Chuck Walbourn Mar 28 '16 at 06:32

1 Answers1

0

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
    );
    
Donald Duck
  • 8,409
  • 22
  • 75
  • 99