Hey guys, I'm pretty new to Direct3D and I try to generate a png image file to disk from a already loaded initialized image from `LPDIRECT3DTEXTURE9` object.
Basically I'm creating a function that get's an std::string for the file destination path and get the pixels from the existing LPDIRECT3DTEXTURE9 object and save them to disk.
Any help would be much appreciated!
Thanks in advance!
EDIT:
I though I should add a sample code of how I do it with BitMap format to get the idea of what I'm trying to do.
Note that this code works, but I try to do something similar with PNG format.
int Width = 128;
int Height = 128;
LPDIRECT3DTEXTURE9 m_lpTexture;
LPDIRECT3DDEVICE9 s_lpD3DDev = NULL;
bool SaveToBitmapFile(const std::string & szPath)
{
LPDIRECT3DSURFACE9 lpSurfSrc = NULL;
LPDIRECT3DSURFACE9 lpSurfDst = NULL;
m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc);
s_lpD3DDev->CreateOffscreenPlainSurface(Width,
Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &lpSurfDst, NULL);
if (D3D_OK !=
D3DXLoadSurfaceFromSurface(lpSurfDst, NULL, NULL, lpSurfSrc,
NULL, NULL, D3DX_FILTER_TRIANGLE, 0))
{
lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;
}
CBitMapFile myBmp;
myBmp.Create(Width, Height);
D3DLOCKED_RECT LR;
lpSurfDst->LockRect(&LR, NULL, 0);
for (int y = 0; y < Height; y++)
{
BYTE* pPixelsSrc = ((BYTE*)LR.pBits) + y * LR.Pitch;
BYTE* pPixelsDst = (BYTE*)(myBmp.Pixels(0, y));
for (int x = 0; x < Width; x++)
{
pPixelsDst[0] = pPixelsSrc[0];
pPixelsDst[1] = pPixelsSrc[1];
pPixelsDst[2] = pPixelsSrc[2];
pPixelsSrc += 4;
pPixelsDst += 3;
}
}
lpSurfDst->UnlockRect();
lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;
HANDLE hFile = ::CreateFile(szPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwRWC = 0;
WriteFile(hFile, &myBmp.m_Header, sizeof(myBmp.m_Header), &dwRWC, NULL);
WriteFile(hFile, &myBmp.m_InfoHeader, sizeof(myBmp.m_InfoHeader), &dwRWC, NULL);
int iWidth = myBmp.Pitch();
for (int y = myBmp.m_InfoHeader.biHeight - 1; y >= 0; y--)
WriteFile(hFile, (BYTE*)myBmp.m_pPixels + y * iWidth, iWidth, &dwRWC, NULL);
CloseHandle(hFile);
}