Jump to content
  • Advertisement
Sign in to follow this  

SharpDX loading TGA textures

This topic is 660 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

Hello, I am trying to load TGA textures in sharpdx, but it seems it is not supported.


Is there any simple method or do I have to write my own TGA format parser, parse the file into pixel array and then use those pixels?


I was trying to use 

var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(

But I get WINCODEC_ERR_COMPONENTNOTFOUND/Componentnotfound on this code, so I presume TGA is not supported.

Share this post

Link to post
Share on other sites

Before rolling your own parser, there are some C# texture IO libraries out there that support loading TGA files.


Shameless plug, because I maintain one - TeximpNet. It wraps two native libraries (FreeImage and Nvidia Texture Tools) in one convenient package for loading and processing textures. FreeImage also has its own C# bindings, but I don't know how well they are maintained.

Share this post

Link to post
Share on other sites

I'm using this code to load TGA-images: https://www.codeproject.com/Articles/31702/NET-Targa-Image-Reader

It's released under CPOL-license, so should be comparable to MIT-license and won't bring problems, when using it

using Paloma;

                var image  = new TargaImage(filename);
                var format = Format.Unknown;
                var imageData = image.Image;
                if(image.Header.PixelDepth==24) {
                    format = Format.B8G8R8A8_UNorm;
                    imageData = image.Image.Clone(new System.Drawing.Rectangle(0, 0, image.Image.Width, image.Image.Height), System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                var bitmapData = imageData.LockBits(new System.Drawing.Rectangle(0, 0, image.Image.Width, image.Image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, imageData.PixelFormat);
                var bufferSize = bitmapData.Height * bitmapData.Stride;

                // copy our buffer to the texture
                int stride = image.Image.Width * 4;
                var tex = new Texture2D(device, new Texture2DDescription() {
                    Width = image.Header.Width,
                    Height = image.Header.Height,
                    Format = format,
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget,
                    Usage = ResourceUsage.Default,
                    CpuAccessFlags = CpuAccessFlags.None,
                    MipLevels = GetNumMipLevels(image.Header.Width,image.Header.Height),
                    OptionFlags = ResourceOptionFlags.GenerateMipMaps,
                    SampleDescription = new SampleDescription(1, 0),

                // unlock the bitmap data

Share this post

Link to post
Share on other sites

Thank you both, will test it once I get over another problem I found.


EDIT: I am using the teximpnet lib and works fine for me.

Edited by satik

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!