BMP (OR JPG) to DDS converter programmatically

3.2k Views Asked by At

I have bmp (or jpg) file. I need to convert it to dds file programmatically (I can use C++, C# with or without .NET; I can try any other language if I would see some clues in it)

I know that there are software that do this, but I need it to integrate into my programm, this should be a part of a longer manipulations on my application.

BTW, my question is: 1) Are there any opensource program that does this, so I can look into the code of it? 2) Are there any tutorials found somewhere in web that can help me to write this code?

I could not found any helpfull information.

Thank you!

1

There are 1 best solutions below

2
On

I'm sure there are standalone converters you can use calling them via command line, if you need a programmatically solution the easiest way I may think about is to use built-in XNA classes to do the job. Because XNA handles all these file formats you can open source .bmp and then save it back to .dds (using the Texture class):

public static void ConvertToDds(
    GraphicsDevice graphicsDevice, string sourcePath, string targetPath)
{
    Texture.FromFile(graphicsDevice, sourcePath)
        .Save(targetPath, ImageFileFormat.Dds);
}

Things changed with XNA 4.0 (these methods have been removed), try the DDSLib to write and Texture2D to read:

public static void ConvertToDds(
    GraphicsDevice graphicsDevice, string sourcePath, string targetPath)
{
    using (var stream = File.Open(sourcePath))
    {
        var texture = Texture2D.FromStream(graphicsDevice, stream);
        DDSLib.DDSToFile(targetPath, true, texture, false); 
    }
}

See linked pages for more details and examples. By the way you can't have C# without .NET!