How to Save a DirectX Image to a File

CraigBlog

Syndication

It's been a really long time since I've done any DirectX writing. So as I was going through my email this weekend, trying to get caught up (sorry to those who've been waiting for answers - I'm getting there!), it was fun to sit down and answer a Direct3D question from a reader. He wanted to know how to save something he'd rendered out to a file. As it turns out, it's dead simple; as long as you set PresentationParameters.SwapEffect to either SwapEffect.Discard or SwapEffect.Copy, you can just use this line of code:

SurfaceLoader.Save(filename, ImageFileFormat.Jpg, _device.GetBackBuffer(0, 0, BackBufferType.Mono));

You have to love Managed DirectX.

I've posted the code here. It's not a full-blown tutorial - those take at least a few full days each to write - just a little sparsely-commented code. Maybe I can start to post a few more little snippets like this on the DirectX wiki to make it more useful.


Posted Feb 05 2005, 09:22 AM by craig-andera

Comments

Oreo wrote re: How to Save a DirectX Image to a File
on 03-27-2005 9:55 AM
Using PNG format instead of Jpg yields much better quality results with about the same size.
matt wrote re: How to Save a DirectX Image to a File
on 03-13-2007 4:37 PM
Fantastic! saved me hours!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?