Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

c# - Converting .dds to .png: is XNA really this convoluted?

I have a .dds file and I want a .png file. Although I already found out about the DevIL.NET library, the API design there of one static class does not parallelize, so I am hoping to find another method. This led me to XNA.

But, here's how far I got with that idea...

  • OK, it looks like I want this Texture2D class; then I can call myTexture2D.SaveAsPng.
  • But how do I get one of those from my .dds file? Well the documentation seems to indicate I want to use myContentManager.Load<Texture2D>.
  • Oh crap, that wasn't it, that's some kind of game content management system. Well, my searching seems to have turned up a lot of uses of myTexture2D.LoadFile; I'll go for that.
  • Uh am I missing an assembly reference or something? Oh no, I get it, they removed that method between 3.1 and 4.0, awesome. OK, well, it's a bit more annoying, but myTexture2D.LoadStream isn't really a problem.
  • Wait what's this now? It wants a GraphicsDevice? Hmm it looks like one usually gets one of those via a GraphicsDeviceManager... oh wait, I'm not going down that path again, no more Managers for me.
  • I guess I'm supposed to instantiate this thing manually. OK well this isn't too hard... var myGraphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, (uh oh what is this PresentationParameters thing well OK I'll just try new PresentationParameters());.
  • Well that threw an exception. They want... a DeviceWindowHandle in my PresentationParameters? BUT I'M RUNNING A CONSOLE APP!!

So I'm really hoping there's a less convoluted way of doing this; perhaps some kind of default GraphicsDevice I could use. It feels pretty silly to create a whole window just to convert .dds to .png.

Alternative suggestions for my conversion problem welcome, I guess, although it would probably be worthwhile to understand how to use XNA from non-XNA code in general.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have a command line app that needs to create an XNA graphics device, the code in this answer should be of some assistance.

In a nutshell, you need some of the classes from the WinForms sample to avoid having to mess around creating a graphics device services and so on (specifically the classes ServiceContainer and GraphicsDeviceService).

Then you can do this:

Form form = new Form(); // Dummy form for creating a graphics device
GraphicsDeviceService gds = GraphicsDeviceService.AddRef(form.Handle,
        form.ClientSize.Width, form.ClientSize.Height);

ServiceContainer services = new ServiceContainer();
services.AddService<IGraphicsDeviceService>(gds);
content = new ContentManager(services, "Content");

Tada - now you have a working ContentManager that you can use to load stuff. I believe you should be able to get the actual GraphicsDevice from the GraphicsDeviceService, too.

The form you create is never displayed. Remember to reference System.Windows.Forms.dll in your project.

Disclaimer: This was written for XNA 3.1. I haven't tested it in 4.0, but I suspect it will work with little or no modification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...