The desktop capture with DirectX does not work

前端 未结 2 1091
情歌与酒
情歌与酒 2021-01-05 22:18

Because processing was slow by D3DPOOL_SCRATCH, I wrote the desktop capture program to reference for the report on the Internet. However, a result is a pitch-black picture.

相关标签:
2条回答
  • 2021-01-05 22:47

    This is nothing related to the application type, if you want to get the data of desktop image, you should use the following function

    GetFrontBufferData

    So instead of calling

    d3ddev->GetRenderTarget(0, &render);
    d3ddev->GetRenderTargetData(render, dest);
    

    You should call

    d3ddev->GetFrontBufferData(0, dest);
    
    0 讨论(0)
  • 2021-01-05 23:07

    If you are running on a version of Windows before Windows 8, the GetFrontBufferData API works fine, as given by another answer. However, starting with Windows 8, there is a new desktop duplication API that can be used to capture the desktop in video memory, including mouse cursor changes and which parts of the screen actually changed or moved. This is far more performant than the D3D9 approach and is really well-suited to doing things like encoding the desktop to a video stream, since you never have to pull the texture out of GPU memory. The new API is available by enumerating DXGI outputs and calling DuplicateOutput on the screen you want to capture. Then you can enter a loop that waits for the screen to update and acquires each frame in turn.

    If you want to encode the frames to a video, I'd recommend taking a look at Media Foundation rather than DirectShow, since Media Foundation has a lot better performance and can use D3D11 rather than D3D9 to do the encoding. It's also a bit easier to use, depending on your scenario. Take a look at Media Foundation's Sink Writer for the simplest method of encoding the video frames.

    For full disclosure, I work on the team that owns the desktop duplication API at Microsoft, and I've personally written apps that capture the desktop (and video, games, etc.) to a video file at 60fps using this technique, as well as a lot of other scenarios.

    0 讨论(0)
提交回复
热议问题