Jump to content

  • Log In with Google      Sign In   
  • Create Account


hopeztm

Member Since 21 Apr 2012
Offline Last Active Private
-----

Topics I've Started

Question About D3DXSaveTextureToFile

21 April 2012 - 07:23 PM

I want to set a texture as render target and then save it to the disk. but the rendering is ok, which means I can draw the tea port on the screen, but the file I saved is not ok, I can't open it with texture format D3DFMT_A8R8G8B8, or the file corropted when I use
D3DFMT_R5G6B5.

the following is my code, I started use DX for 1 week, I am really weak in this technique, thank you so much~



HRESULT DXMainFrame::FrameSnapShot(HWND hWnd, TCHAR *filename)
{
D3DDISPLAYMODE mode;
HRESULT hr;
if (FAILED(hr = m_pD3DDevice->GetDisplayMode(0, &mode)))
return hr;
IDirect3DTexture9 * g_pRenderTexture = NULL;
hr = m_pD3DDevice->CreateTexture(mode.Width,mode.Height,1,
D3DUSAGE_RENDERTARGET,
D3DFMT_R5G6B5,
D3DPOOL_DEFAULT,&g_pRenderTexture,
NULL);
LPDIRECT3DSURFACE9 surf,oldRenderTarget;
hr = g_pRenderTexture->GetSurfaceLevel(0,&surf);
m_pD3DDevice->GetRenderTarget(0,&oldRenderTarget);
m_pD3DDevice->SetRenderTarget(0,surf);
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR
(0.0f,0.25f,0.25f,0.55f), 1.0f, 0);
drawTeaPort(m_pD3DDevice);
m_pD3DDevice->SetRenderTarget(0,oldRenderTarget);
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR (0.0f,0.25f,0.25f,0.55f), 1.0f, 0);
OnFrameRender(m_pD3DDevice,DXUTGetTime(),DXUTGetElapsedTime(),NULL);
hWnd = DXUTGetHWND();


// area to capture
RECT *rect = NULL;
WINDOWINFO windowInfo;
windowInfo.cbSize =

sizeof(WINDOWINFO);


if(hWnd) // capture windo
{
GetWindowInfo(hWnd, &windowInfo);
rect = &windowInfo.rcWindow;
}
hr = D3DXSaveTextureToFile(filename,D3DXIFF_BMP,g_pRenderTexture,NULL);


//hr = D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, surf, NULL, rect); didn't work either
surf->Release();
g_pRenderTexture->Release();


return hr;
}

PARTNERS