Magick.NET
| Language | C# (.NET) |
|---|---|
| License | Apache 2.0 (ImageMagick itself: ImageMagick License) |
| Repository | github.com/dlemstra/Magick.NET |
| Documentation | docs folder on GitHub |
| Depends on | native ImageMagick binaries, included in the NuGet package |
| Read / write | yes / yes, all TGA image types, raw or RLE |
Magick.NET wraps ImageMagick for .NET (Windows, Linux and macOS, x64 and arm64). It reads palette, grayscale and true-color TGA files, raw and RLE, and writes TGA with or without RLE. The same API handles 100+ other formats, so it’s a good choice for conversion services.
The package comes in Q8 (8 bits per channel) and Q16 (16 bits per channel) builds. TGA is 8 bits per channel, so the smaller and faster Q8 build is enough unless you also process high bit depth formats.
Installation#
Shell
dotnet add package Magick.NET-Q8-AnyCPU
Basic usage: convert TGA to PNG#
C#
using ImageMagick;
using var image = new MagickImage("in.tga");
Console.WriteLine($"{image.Width} x {image.Height}, alpha: {image.HasAlpha}");
image.Write("out.png"); // format from the extension
How to save a TGA with RLE compression#
C#
using ImageMagick;
using var image = new MagickImage("in.png");
image.Settings.Compression = CompressionMethod.RLE; // CompressionMethod.NoCompression for raw
image.Write("out.tga", MagickFormat.Tga);
How to get TGA width and height without decoding#
C#
using ImageMagick;
var info = new MagickImageInfo("in.tga");
Console.WriteLine($"{info.Width} x {info.Height}, {info.Format}");
How to read a TGA from a stream or a file without the .tga extension#
TGA has no magic bytes, so tell Magick.NET the format:
C#
using ImageMagick;
var settings = new MagickReadSettings { Format = MagickFormat.Tga };
using var stream = File.OpenRead("texture.bin");
using var image = new MagickImage(stream, settings);
How to write TGA to a byte array#
C#
using var image = new MagickImage("in.png");
byte[] tga = image.ToByteArray(MagickFormat.Tga);
Tips#
WidthandHeightareuintin current versions.- Strip metadata you don’t need with
image.Strip()before writing.