TargaSharp.Drawing 0.4.0

dotnet add package TargaSharp.Drawing --version 0.4.0
                    
NuGet\Install-Package TargaSharp.Drawing -Version 0.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TargaSharp.Drawing" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TargaSharp.Drawing" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="TargaSharp.Drawing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TargaSharp.Drawing --version 0.4.0
                    
#r "nuget: TargaSharp.Drawing, 0.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TargaSharp.Drawing@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TargaSharp.Drawing&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=TargaSharp.Drawing&version=0.4.0
                    
Install as a Cake Tool

TargaSharp

CI

A .NET library for reading, editing and writing Truevision TGA (.tga) image files, covering the full TGA 2.0 specification: header, color map, raw and run-length-encoded image data, developer area, extension area (scan-line table, postage stamp, color-correction table) and footer.

Derived from TGASharpLib by Alex Green.

Packages

Package Targets Purpose
TargaSharp net48, net6.0, net8.0, net10.0 Core: file model, reader, writer, validation. No native or OS-specific dependencies.
TargaSharp.Drawing net48, net6.0, net8.0, net10.0 (Windows) System.Drawing.BitmapTgaFile conversion via GDI+.
dotnet add package TargaSharp
dotnet add package TargaSharp.Drawing   # optional, Windows only

Usage

Read, edit, write

using TargaSharp;

var tga = new TgaFile("input.tga");          // or new TgaFile(byte[]) / new TgaFile(Stream)

ushort width  = tga.Width;
byte[] pixels = tga.ImageArea.ImageData;      // decoded, BGR(A) byte order, row-major
var origin    = tga.Header.ImageSpec.ImageDescriptor.ImageOrigin;

tga.ExtensionArea!.AuthorName = new TgaString("me", 41, true);
tga.Flip(vertical: true);                     // toggles the origin bit; pixel data is untouched
tga.ToOldFormat();                            // drop footer/extension/developer areas -> plain TGA 1.0; ToNewFormat() is the inverse

tga.Save("output.tga");                       // or tga.Save(Stream) / byte[] bytes = tga.ToBytes()

Create from scratch

var tga = new TgaFile(256, 256, TgaPixelDepth.Bpp32, TgaImageType.RleTrueColor, attrBits: 8);
FillPixels(tga.ImageArea.ImageData);          // 256 * 256 * 4 bytes, B G R A per pixel
tga.Save("new.tga");

Validate before writing

foreach (TgaValidationError e in tga.Validate())
    Console.WriteLine($"{e.Path}: {e.Message}");

Save/ToBytes run the same validation and throw TgaValidationException (with the full error list) rather than writing an inconsistent file. Reading is lenient: files with out-of-spec metadata still load; only malformed structure raises TgaFormatException.

Reader / writer interfaces

using TargaSharp.IO;

ITgaReader reader = new TgaReader();
TgaFile tga = reader.Read(stream);

ITgaWriter writer = new TgaWriter(new TgaValidator());   // validator is injectable
writer.Write(tga, stream);                               // any writable stream, seeking not required

Bitmaps (TargaSharp.Drawing)

using TargaSharp.Drawing;

TgaFile tga = TgaDrawing.FromBitmap(bitmap, useRle: true);
using Bitmap bmp = tga.ToBitmap();
using Bitmap? thumb = tga.GetPostageStampBitmap();

Supported formats

Image type Uncompressed RLE
Color-mapped (8-bit index, 15/16/24/32-bit palette)
True-color 16 (A1R5G5B5), 24, 32 bpp
Grayscale 8 bpp, 16 bpp (8 + 8 alpha)

All four image origins (bottom-left, bottom-right, top-left, top-right), legacy TGA 1.0 files without a footer, and every TGA 2.0 extension field are read and written.

Errors

Exception When
TgaFormatException Input is not a well-formed TGA (truncated, sizes don't add up)
TgaValidationException The in-memory file violates the spec and cannot be written; .Errors lists every violation
ArgumentException / ArgumentOutOfRangeException A field setter was given an out-of-range value (ASCII-only strings, 4×80 comment lines, alpha bits ≤ 15, …)
NotSupportedException TgaDrawing.FromBitmap given a PixelFormat it cannot convert

Both TGA exceptions derive from TgaException.

Building

dotnet build TargaSharp.sln
dotnet test TargaSharp.sln

Requires the .NET 10 SDK (global.json). dotnet build -c Release also produces the NuGet packages.

License

MIT — see LICENSE.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 40 9/15/2026
0.3.0 49 9/14/2026

0.3.0: net48 target, ToOldFormat(), stricter validation and model invariants. See CHANGELOG.md for breaking changes.