如何将IDirect3DSurface9或LPD3DXBUFFER作为JPG保存到内存中

时间:2014-05-11 10:41:55

标签: c++ jpeg directx-9

我试图捕捉游戏截图,我有那个代码:

LPDIRECT3DDEVICE9 Device;
D3DSURFACE_DESC screenDescription;

...

void Capture(){
    IDirect3DSurface9* pRenderTarget;
    IDirect3DSurface9* pDestTarget;

    Device->GetRenderTarget(0, &pRenderTarget);
    pRenderTarget->GetDesc(&screenDescription);

    Device->CreateOffscreenPlainSurface(screenDescription.Width, screenDescription.Height, screenDescription.Format, D3DPOOL_SYSTEMMEM, &pDestTarget, NULL);
    Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_FORCE_DWORD, &pDestTarget);

    char tName[200];
    sprintf(tName, "%s_%d.jpg", "C:\\test", GetTickCount());
    D3DXSaveSurfaceToFileA(tName, D3DXIFF_JPG, pDestTarget, NULL, NULL);

    //LPD3DXBUFFER buffer;
    //D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_JPG, pDestTarget, NULL, NULL);

    pRenderTarget->Release();
    pDestTarget->Release();

    isCapturing = false;
}

使用D3DXSaveSurfaceToFileA保存到文件工作正常,但我想保存捕获的图像并将其写入磁盘上另一个文件的末尾,而不是创建新的一次。 有没有办法将IDirect3DSurface9LPD3DXBUFFER转换为 JPG 字节?

1 个答案:

答案 0 :(得分:2)

好的,我发现了这段代码并且他工作了:

LPD3DXBUFFER buffer;
D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_JPG, pDestTarget, NULL, NULL);
DWORD imSize = buffer->GetBufferSize();
void* imgBuffer = buffer->GetBufferPointer();

std::fstream out;
out.open(tName, std::ios_base::binary | std::ios_base::out);
out.write((char*)imgBuffer, imSize);
out.clear();
out.close();