Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DirectX to capture screen on Windows

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.

like image 236
Hrishikesh_Pardeshi Avatar asked Jul 23 '13 13:07

Hrishikesh_Pardeshi


People also ask

Does the desktop capture with DirectX work?

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.

What is screen capture in Windows 10?

Screen capture supports scenarios like screen recording for eLearning, screen sharing for collaboration, game streaming, remote diagnostics, and taking screen shots for visual comparison or editing. The new UWP WindowsGraphicsCapture APIs provide a modern, performant way of capturing screen contents in Win32 and UWP applications.

How do I take a screenshot of my screen?

Look for the “Capture” widget window in the overlay. There are four buttons on the Capture widget (from left to right): Screenshot: Takes a screenshot of the active window. Record last 30 seconds: Creates a recording of the previous 30 seconds. Start recording: Starts recording your active window.

How to get DirectX SDK for Windows XP?

You can get the DirectX SDK from microsoft.com/en-ca/download/details.aspx?id=6812 (posted by @yms). This SDK is compatible with all versions Windows, including XP. Refer to its documentation on how to include/link with D3D9. In your example Device is an IDirect3DDevice9.


1 Answers

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.

like image 77
zdd Avatar answered Sep 28 '22 03:09

zdd