c++hookdirect3d

D3DXSaveTextureToFileW doesn't working properly


It seems that the screenshot is not being generated properly after this code:

IDirect3DBaseTexture9* pBaseTexture = nullptr;
pDevice->GetTexture(0, &pBaseTexture);

if (pBaseTexture != NULL) {

    std::cout << "Saving screenshot..." << std::endl;

    const wchar_t* fileName = L"C:\\Screenshots\\pBaseTexture.png";
    D3DXSaveTextureToFileW(fileName, D3DXIFF_PNG, pBaseTexture, nullptr);
}

I'm trying to print an image using a d3d9 hook.


Solution

  • If you don't want use the D3DX9. You can use stb_image_write.h instead.

    D3DSURFACE_DESC desc;
    pBackBuffer->GetDesc(&desc);
    
    int width = desc.Width;
    int height = desc.Height;
    
    int bufferSize = width * height * 4;
    unsigned char* imageData = new unsigned char[bufferSize];
    
    D3DLOCKED_RECT lockedRect;
    pBackBuffer->LockRect(&lockedRect, NULL, D3DLOCK_READONLY);
    
    unsigned char* sourceData = reinterpret_cast<unsigned char*>(lockedRect.pBits);
    memcpy(imageData, sourceData, bufferSize);
    
    int jpeg_quality = 90;
    stbi_write_jpg(fullPath, width, height, 4, imageData, jpeg_quality);