Sign in to follow this  

Question About D3DXSaveTextureToFile

This topic is 2066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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][size=3][font=Courier New][size=3]D3DFMT_A8R8G8B8, or the file corropted when I use [/size][/font][/size][/font]
[font=Courier New][size=3][font=Courier New][size=3][font=Courier New][size=3][font=Courier New][size=3]D3DFMT_R5G6B5. [/size][/font][/size][/font][/size][/font][/size][/font]

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



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

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


[font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"][font="Courier New"][size="3"][color="#0000ff"] if[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Courier New"][size="3"][font="Courier New"][size="3"](hWnd) [/size][/font][/size][/font][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"][font="Courier New"][size="3"][color="#008000"]// capture windo[/color][/size][/font][/color][/size][/font][/color][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] {[/size][/font][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] GetWindowInfo(hWnd, &windowInfo);[/size][/font][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] rect = &windowInfo.rcWindow;[/size][/font][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] }[/size][/font][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] hr = D3DXSaveTextureToFile(filename,D3DXIFF_BMP,g_pRenderTexture,NULL);[/size][/font][/size][/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[/color][/size][/font][/color][/size][/font][/color][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] surf->Release();[/size][/font][/size][/font]
[font="Courier New"][size="3"][font="Courier New"][size="3"] g_pRenderTexture->Release();[/size][/font][/size][/font]


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

Share this post


Link to post
Share on other sites
[b]Hi[/b]
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:
[CODE]
D3DXSaveSurfaceToFile(filename, D3DXIFF_BMP, surf, NULL, NULL)
[/CODE]

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

Share this post


Link to post
Share on other sites

This topic is 2066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this