title: Using DirectX to capture screen on Windows
body: <p>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 i5@2.6 GHz, 8 GB RAM).</p>
  
  <p>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).</p>
  
  <p>I went through this post: <a href="https://stackoverflow.com/questions/5069104/fastest-method-of-screen-capturing">Fastest method of screen capturing</a> 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:</p>
  
  <pre><code>    IDirect3DSurface9* pRenderTarget=NULL;
      IDirect3DSurface9* pDestTarget=NULL;
      // sanity checks.
      if (g_pd3dDevice == NULL){
        return;
      }
      HRESULT hr;
      // get the render target surface.
      hr = g_pd3dDevice-&gt;GetRenderTarget(0, &amp;pRenderTarget);
      // get the current adapter display mode.
      //hr = pDirect3D-&gt;GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&amp;d3ddisplaymode);
  
      // create a destination surface.
      hr = g_pd3dDevice-&gt;CreateOffscreenPlainSurface(1600, 900, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &amp;pDestTarget, NULL);
     //copy the render target to the destination surface.
     hr = g_pd3dDevice-&gt;GetRenderTargetData(pRenderTarget, pDestTarget);
  
     D3DLOCKED_RECT lockedRect;
     hr =pDestTarget-&gt;LockRect(&amp;lockedRect,NULL, D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_NOSYSLOCK|D3DLOCK_READONLY);
  
      for( int i=0 ; i &lt; 900 ; i++)
      {
          for(int j=0; j &lt; 1600; j++){
              memcpy( (BYTE*) data + i * 1600 * 3 + j * 3, (BYTE*) lockedRect.pBits + i* lockedRect.Pitch + j * 4,  3);
          }
      }
      pDestTarget-&gt;UnlockRect();
     // clean up.
     pRenderTarget-&gt;Release();
     pDestTarget-&gt;Release();
  </code></pre>
  
  <p>And for the device Initialization part:</p>
  
  <pre><code>g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION); 
  D3DPRESENT_PARAMETERS PresentParams; 
  memset(&amp;PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS)); 
  PresentParams.Windowed = TRUE; 
  PresentParams.SwapEffect =D3DSWAPEFFECT_DISCARD;
  g_pDirect3D-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL ,GetDesktopWindow(),D3DCREATE_SOFTWARE_VERTEXPROCESSING, &amp;PresentParams,&amp;g_pd3dDevice);
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>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: <a href="https://stackoverflow.com/questions/12859894/the-desktop-capture-with-directx-does-not-work/12863143#12863143">The desktop capture with DirectX does not work</a> and <a href="https://stackoverflow.com/questions/12879880/how-to-get-the-screenshot-of-the-desktop-from-back-buffer-with-directx?rq=1">How to get the screenshot of the desktop from back buffer with DirectX</a>.</p>
  
  <p>However, any suggestions to a faster method for capturing desktop screen or optimizing the GDI way are welcome.</p>