title: Get pixels from IDirect3DSurface9?
body: <p>I want to get a fast access to the pixels of a game which uses directX9 or 10. Therefore I used the D3D9 library. Do I have to use the D3D10 library when it is a directX10 game? If this is true, the next part is irrelevant. </p>
  
  <p>The following code works except I'm only get zeros in <code>qDebug("%.2f, %.2f, %.2f, %.2f", pixel[0].r, pixel[0].g, pixel[0].b, pixel[0].a);</code> although it should have an other color and I have no clue why. Is there an error in swapping the data?</p>
  
  <p><strong>Initialization:</strong></p>
  
  <pre><code>IDirect3D9 *m_d3d;
  IDirect3DDevice9 *m_d3ddev;
  D3DDISPLAYMODE *m_d3ddm;
  HRESULT hr;
  m_d3d = Direct3DCreate9(D3D_SDK_VERSION);
  if (m_d3d == NULL)
  {
      qFatal("Cannot create Direct3D!");
  }
  
  m_d3ddm = (D3DDISPLAYMODE *)calloc(1, sizeof(D3DDISPLAYMODE));
  hr = m_d3d-&gt;GetAdapterDisplayMode(D3DADAPTER_DEFAULT, m_d3ddm);
  if (hr != D3D_OK)
  {
      m_d3d-&gt;Release();
      qFatal("Cannot get DisplayMode!");
  }
  
  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&amp;d3dpp, sizeof(d3dpp));
  d3dpp.Windowed = TRUE;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.hDeviceWindow = m_target;
  d3dpp.BackBufferWidth = m_d3ddm-&gt;Width;
  d3dpp.BackBufferHeight = m_d3ddm-&gt;Height;
  d3dpp.BackBufferFormat = m_d3ddm-&gt;Format;
  
  hr = m_d3d-&gt;CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_target, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &amp;d3dpp, &amp;m_d3ddev);
  if (FAILED(hr))
  {
      m_d3d-&gt;Release();
      qFatal("Failed  CreateDevice!");
  }
  else 
  {
      qDebug("Created Device with %i * %i", m_d3ddm-&gt;Width, m_d3ddm-&gt;Height);
  }
  </code></pre>
  
  <p><strong>Capture:</strong></p>
  
  <pre><code>IDirect3DSurface9 *renderTarget= NULL;
  IDirect3DSurface9 *destTarget = NULL;
  hr = m_d3ddev-&gt;GetRenderTarget(0, &amp;renderTarget);
  if (FAILED(hr))
  {
      qFatal("Failed  GetRenderTarget!");
  }
  hr = m_d3ddev-&gt;CreateOffscreenPlainSurface(m_d3ddm-&gt;Width, m_d3ddm-&gt;Height, m_d3ddm-&gt;Format, D3DPOOL_SYSTEMMEM, &amp;destTarget, NULL);
  if (FAILED(hr))
  {
      qFatal("Failed  CreateOffscreenPlainSurface!");
  }
  hr = m_d3ddev-&gt;GetRenderTargetData(renderTarget, destTarget);
  if (FAILED(hr))
  {
      qFatal("Failed  GetRenderTargetData!");
  }
  
  D3DLOCKED_RECT lr;
  ZeroMemory(&amp;lr, sizeof(D3DLOCKED_RECT));
  hr = destTarget-&gt;LockRect(&amp;lr, 0, D3DLOCK_READONLY);
  if (FAILED(hr))
  {
      qFatal("Cannot lock rect!");
  }
  ARGB *pixel = (ARGB *)lr.pBits;
  if (!pixel)
  {
      qFatal("No data!");
  }
  qDebug("%.2f, %.2f, %.2f, %.2f", pixel[0].r, pixel[0].g, pixel[0].b, pixel[0].a);
  
  hr = destTarget-&gt;UnlockRect();
  if (FAILED(hr))
  {
      qFatal("Cannot unlock rect!");
  }
  renderTarget-&gt;Release();
  destTarget-&gt;Release(); 
  </code></pre>
  
  <p><strong>Cleaning</strong></p>
  
  <pre><code>m_d3ddev-&gt;Release();
  m_d3d-&gt;Release();
  
  delete m_d3ddm;
  delete m_d3d;
  delete m_d3ddev;
  </code></pre>