TargaSharp.Drawing
0.3.0
See the version list below for details.
dotnet add package TargaSharp.Drawing --version 0.3.0
NuGet\Install-Package TargaSharp.Drawing -Version 0.3.0
<PackageReference Include="TargaSharp.Drawing" Version="0.3.0" />
<PackageVersion Include="TargaSharp.Drawing" Version="0.3.0" />
<PackageReference Include="TargaSharp.Drawing" />
paket add TargaSharp.Drawing --version 0.3.0
#r "nuget: TargaSharp.Drawing, 0.3.0"
#:package TargaSharp.Drawing@0.3.0
#addin nuget:?package=TargaSharp.Drawing&version=0.3.0
#tool nuget:?package=TargaSharp.Drawing&version=0.3.0
TargaSharp
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.Bitmap ↔ TgaFile 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 | Versions 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. |
-
.NETFramework 4.8
- TargaSharp (>= 0.3.0)
-
net10.0
- System.Drawing.Common (>= 10.0.12)
- TargaSharp (>= 0.3.0)
-
net6.0
- System.Drawing.Common (>= 8.0.10)
- TargaSharp (>= 0.3.0)
-
net8.0
- System.Drawing.Common (>= 8.0.10)
- TargaSharp (>= 0.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
0.3.0: net48 target, ToOldFormat(), stricter validation and model invariants. See CHANGELOG.md for breaking changes.