Debugging a DirectX application is not the easiest thing to do. Most times, you’re looking for reasons why your image did not display on the screen or why it displayed incorrectly. The following snippet of code should help in your debugging efforts. Cut and paste it to your DirectX program and call it before calling the DirectX Present() method. It will allow you to capture an image of a screen buffer into a bitmap file. Each image is named in a sequential fashion. This will provide a snapshot of exactly what is being presented to the screen.
- This code assumes you have one back buffer defined.
- This code assumes your DirectX device is defined as ‘windowed’.
- m_rect is the CRect for the associated device window.
// This method is used for debugging. It will dump the current
// back buffer surface to a bitmap. This provides a snapshot of
// what is currently ready for presentation.
// sanity checks.
if (pDirect3DDevice == NULL)
// get the render target surface.
HRESULT hr = pDirect3DDevice->GetRenderTarget(0, &pRenderTarget);
// get the current adapter display mode.
hr = pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
// create a destination surface.
hr = pDirect3DDevice->CreateOffscreenPlainSurface(m_rect.Width(),
//copy the render target to the destination surface.
hr = pDirect3DDevice->GetRenderTargetData(pRenderTarget,
//save its contents to a bitmap file.
file.Format(“DX Buffer %d.bmp”, ndebug++);
hr = D3DXSaveSurfaceToFile(file,
// clean up.