Using DirectX to capture screen on Windows

10,702

Did you render anything before taking the screenshot?

  • GetFrontBufferData - this function get the data of the screen
  • GetRenderTargetData - this function get the data from render target

So if you didn't render anything, you definitely will got a blank image.

Share:
10,702
Hrishikesh_Pardeshi
Author by

Hrishikesh_Pardeshi

Updated on June 04, 2022

Comments

  • Hrishikesh_Pardeshi
    Hrishikesh_Pardeshi almost 2 years

    I was experimenting with ways to capture screen on windows and consequently deciding on the fastest way to do so. The most common is obviously the GDI way. And it is decent in performance as well. Depending on the system load and static/non-static screen content, the screen capture rate ranged from 27-47 fps (Windows 7, Intel [email protected] GHz, 8 GB RAM).

    Now, with DirectX front buffer approach(using the GetFrontBufferData() API), the performance was comparable but slightly on the slower side(I couldn't reach as high as 48 fps).

    I went through this post: Fastest method of screen capturing and the tried out the way suggested in the accepted answer using getRenderTarget() and getRenderTargetData(), but as suggested in the comments, all I get is a black image. Here is my complete code including initial configurations for the device:

        IDirect3DSurface9* pRenderTarget=NULL;
        IDirect3DSurface9* pDestTarget=NULL;
        // sanity checks.
        if (g_pd3dDevice == NULL){
          return;
        }
        HRESULT hr;
        // get the render target surface.
        hr = g_pd3dDevice->GetRenderTarget(0, &pRenderTarget);
        // get the current adapter display mode.
        //hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddisplaymode);
    
        // create a destination surface.
        hr = g_pd3dDevice->CreateOffscreenPlainSurface(1600, 900, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pDestTarget, NULL);
       //copy the render target to the destination surface.
       hr = g_pd3dDevice->GetRenderTargetData(pRenderTarget, pDestTarget);
    
       D3DLOCKED_RECT lockedRect;
       hr =pDestTarget->LockRect(&lockedRect,NULL, D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY);
    
        for( int i=0 ; i < 900 ; i++)
        {
            for(int j=0; j < 1600; j++){
                memcpy( (BYTE*) data + i * 1600 * 3 + j * 3, (BYTE*) lockedRect.pBits + i* lockedRect.Pitch + j * 4,  3);
            }
        }
        pDestTarget->UnlockRect();
       // clean up.
       pRenderTarget->Release();
       pDestTarget->Release();
    

    And for the device Initialization part:

    g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); 
    D3DPRESENT_PARAMETERS PresentParams; 
    memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS)); 
    PresentParams.Windowed = TRUE; 
    PresentParams.SwapEffect =D3DSWAPEFFECT_DISCARD;
    g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL ,GetDesktopWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams,&g_pd3dDevice);
    

    Please point out the mistake in the above code. Any suggestions are welcome. I also tried the GetBackBuffer() API but I couldn't get hold of the image data.

    EDIT:

    My use case was to capture desktop screen which seems to be done only using GetFrontBufferData() via directX. GetBackBuffer()/GetRenderTargetData() is not the correct API to call for this case. Reference: The desktop capture with DirectX does not work and How to get the screenshot of the desktop from back buffer with DirectX.

    However, any suggestions to a faster method for capturing desktop screen or optimizing the GDI way are welcome.