Saturday, February 5, 2005

How to Save a DirectX Image to a File

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.

2 comments:

  1. Using PNG format instead of Jpg yields much better quality results with about the same size.

    ReplyDelete
  2. Fantastic! saved me hours!

    ReplyDelete