title: How to save IDirect3DSurface9 or LPD3DXBUFFER to memory as JPG
body: <p>I'm trying to capture game screenshot, i have that code:</p>
  
  <pre><code>LPDIRECT3DDEVICE9 Device;
  D3DSURFACE_DESC screenDescription;
  
  ...
  
  void Capture(){
      IDirect3DSurface9* pRenderTarget;
      IDirect3DSurface9* pDestTarget;
  
      Device-&gt;GetRenderTarget(0, &amp;pRenderTarget);
      pRenderTarget-&gt;GetDesc(&amp;screenDescription);
  
      Device-&gt;CreateOffscreenPlainSurface(screenDescription.Width, screenDescription.Height, screenDescription.Format, D3DPOOL_SYSTEMMEM, &amp;pDestTarget, NULL);
      Device-&gt;GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_FORCE_DWORD, &amp;pDestTarget);
  
      char tName[200];
      sprintf(tName, "%s_%d.jpg", "C:\\test", GetTickCount());
      D3DXSaveSurfaceToFileA(tName, D3DXIFF_JPG, pDestTarget, NULL, NULL);
  
      //LPD3DXBUFFER buffer;
      //D3DXSaveSurfaceToFileInMemory(&amp;buffer, D3DXIFF_JPG, pDestTarget, NULL, NULL);
  
      pRenderTarget-&gt;Release();
      pDestTarget-&gt;Release();
  
      isCapturing = false;
  }
  </code></pre>
  
  <hr>
  
  <p>Saving to file with <code>D3DXSaveSurfaceToFileA</code> works perfectly, but i want to save captured image and write them to end of another file on disk, not creating a new once.
  Is there a way how convert <code>IDirect3DSurface9</code> or <code>LPD3DXBUFFER</code> to <strong>JPG</strong> bytes?</p>