// create a texture to store the screenshot
IDirect3DTexture9* pTexture = NULL;
HRESULT hr = pDevice->CreateTexture(
SCREEN_WIDTH, SCREEN_HEIGHT, 1, D3DUSAGE_RENDERTARGET,
D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL);
// save the current render target and set your texture as the render target
IDirect3DSurface9* pSurface;
pDevice->GetRenderTarget(0, &pSurface);
IDirect3DSurface9* pTextureSurface;
pTexture->GetSurfaceLevel(0, &pTextureSurface);
pDevice->SetRenderTarget(0, pTextureSurface);
// render your scene and all ImGui windows
// ...
// copy the render target to your texture
RECT rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
pDevice->StretchRect(pSurface, &rect, pTextureSurface, &rect, D3DTEXF_NONE);
// restore the original render target
pDevice->SetRenderTarget(0, pSurface);
// access the texture data
D3DLOCKED_RECT lockedRect;
pTexture->LockRect(0, &lockedRect, NULL, D3DLOCK_READONLY);
BYTE* pBits = (BYTE*)lockedRect.pBits;
// use pBits to access the texture data
// release resources
pTextureSurface->Release();
pSurface->Release();
pTexture->Release();