title: How to get the screenshot of the desktop from back buffer with DirectX
body: <p>I asked <a href="https://stackoverflow.com/questions/12859894/the-desktop-capture-with-directx-does-not-work">how to take the screen shot in DirectX</a> and "Use GetFrontBufferData()" was answered. However, "This function is very slow, by design, and should not be used in any performance-critical path" was described at <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb174388%28v=vs.85%29.aspx" rel="nofollow noreferrer">MSDN</a>. </p>
  
  <p>When I asked other persons the measure, "Transfer the data of a back buffer to the system memory" was answered. But, I was not able to find a concrete method.</p>
  
  <p>This is an example of failure. Please let me know the right code. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;d3d9.h&gt;
  #include &lt;d3dx9tex.h&gt;
  
  void main()
  {
      CoInitialize(NULL);
  
      HWND hwnd = GetDesktopWindow();
  
      LPDIRECT3D9 d3d9;
      D3DDISPLAYMODE ddm;
  
      d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
      d3d9-&gt;GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&amp;ddm);
  
      D3DPRESENT_PARAMETERS d3dpp;
      ZeroMemory(&amp;d3dpp,sizeof(D3DPRESENT_PARAMETERS));
      d3dpp.Windowed = TRUE;
      d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
      d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
      d3dpp.BackBufferCount = 1;
      d3dpp.BackBufferWidth = ddm.Width;
      d3dpp.BackBufferHeight = ddm.Height;
      d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
      d3dpp.MultiSampleQuality = 0;
      d3dpp.EnableAutoDepthStencil = TRUE;
      d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
      d3dpp.hDeviceWindow = hwnd;
      d3dpp.Flags = D3DPRESENTFLAG_VIDEO;
      d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
      d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  
      LPDIRECT3DDEVICE9 dev;
      d3d9-&gt;CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING ,&amp;d3dpp, &amp;dev);
  
      /* Deprecation
      IDirect3DSurface9* surface = NULL;
      dev-&gt;CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &amp;surface, NULL);
      dev-&gt;GetFrontBufferData(0, surface);
      D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, surface, NULL, NULL);
      */
  
      // Transfer the data of a back buffer to the system memory
      IDirect3DSurface9* surface = NULL;
      dev-&gt;CreateOffscreenPlainSurface(ddm.Width, ddm.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &amp;surface, NULL);
      LPDIRECT3DSURFACE9 back = NULL;
      dev-&gt;SetRenderTarget(0, surface);
      dev-&gt;GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &amp;back);
      dev-&gt;Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,127), 1.0, 0 );
      dev-&gt;BeginScene();
      dev-&gt;StretchRect(surface, NULL, back, NULL, D3DTEXF_NONE);
      dev-&gt;EndScene();
      dev-&gt;Present(0,0,0,0);
      D3DXSaveSurfaceToFile(L"D:\\test.bmp", D3DXIFF_BMP, back, NULL, NULL);
  
  
      if(back) back-&gt;Release();
  
      dev-&gt;Release();
      d3d9-&gt;Release();
  
      CoUninitialize();
  }
  </code></pre>