Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question About D3DXSaveTextureToFile

Started by hopeztm Apr 22, 2012 at 1:23 AM 1 replies 2.9k views
Original Post
hopeztm
hopeztm
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 [font=Courier New]

[font=Courier New]

D3DFMT_A8R8G8B8, or the file corropted when I use [/font][/font]
[font=Courier New]

[font=Courier New]

[font=Courier New]

[font=Courier New]

D3DFMT_R5G6B5. [/font][/font][/font][/font]

[font=Courier New]

[font=Courier New]

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



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


[font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"]// area to capture[/font][/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] RECT *rect = NULL;[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] WINDOWINFO windowInfo;[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] windowInfo.cbSize = [/font][/font]

[font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] sizeof[/font][/font][/font][font="Courier New"][size="3"][font="Courier New"][size="3"](WINDOWINFO);[/font][/font]


[font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] if[/font][/font][/font][font="Courier New"][size="3"][font="Courier New"][size="3"](hWnd) [/font][/font][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"]// capture windo[/font][/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] {[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] GetWindowInfo(hWnd, &windowInfo);[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] rect = &windowInfo.rcWindow[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] }[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] hr = D3DXSaveTextureToFile(filename,D3DXIFF_BMP,g_pRenderTexture,NULL);[/font][/font]


[font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"] //hr = D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, surf, NULL, rect); didn't work either[/font][/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] surf->Release();[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] g_pRenderTexture->Release();[/font][/font]


[font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] return[/font][/font][/font][font="Courier New"][size="3"][font="Courier New"][size="3"] hr;[/font][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"]}[/font][/font]

hopeztm
hopeztm
thnks
kubera
kubera
Hi
Please consider checking HRESULT (hr), you could put this returned value into the DXErr.exe (in the SDK) and watch what the error means.
You would run your application in Debug Mode inside Visual Studio. The console could print a warning for this situation.
Maybe you would save BITMAP this way:

D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, surf, NULL, NULL)


P. S.
You have a bit weak checking of errors. It would be possible, that the error is earlier.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.