SixLabors/ImageSharp · error · InvalidImageContentException

{errorMessage}

Error message

{errorMessage}

What it means

TgaThrowHelper.ThrowInvalidImageContentException(string) throws InvalidImageContentException with a caller-provided message. It is the TGA decoder's standard guard for content that violates TGA expectations: non-positive dimensions, missing/oversized color maps, unsupported combinations of image type and pixel depth, and per-scanline length mismatches. The exact reason is always carried in the message string.

Solutions

  1. Read the exception message for the specific violation (dimensions, color map, depth) and inspect the 18-byte TGA header accordingly.
  2. Re-export or convert the file from its source application into a standard TGA or PNG.
  3. Pre-check file size against header-declared dimensions before decoding large/old files.
  4. Catch InvalidImageContentException and route the file to a repair/conversion path.

Example fix

// before
using var image = Image.Load("art.tga");
// after
try
{
    using var image = Image.Load("art.tga");
}
catch (InvalidImageContentException ex)
{
    Console.WriteLine($"TGA content invalid: {ex.Message}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Basic TGA header triage before decode.
byte[] h = new byte[18];
using (var fs = File.OpenRead(path)) if (fs.Read(h, 0, 18) != 18) throw new InvalidDataException("Too short.");
byte idLen = h[0], cmLen = h[5];
int w = h[12] | (h[13] << 8), ht = h[14] | (h[15] << 8);
if (w == 0 || ht == 0) throw new InvalidDataException("TGA has zero dimensions.");
if (h[1] == 1 && cmLen == 0) throw new InvalidDataException("Color-mapped TGA without color map length.");

Try / catch

try { using var img = Image.Load(path); }
catch (InvalidImageContentException ex) { Console.WriteLine($"Reject TGA: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling by TgaDecoderCore when header checks fail, e.g. width or height <= 0 (line 77), missing color-map length (line 87), unsupported type/depth combinations, or scanline data not matching the declared geometry (lines ~407-921).

Common situations: Zero-sized TGA files produced by crashed tools; color-mapped images with truncated palettes; TGA variants with unexpected pixel depths (e.g. unsupported bit counts); corrupted downloads.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/2ce78029f0fc33b2. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Tga/TgaThrowHelper.cs:9

// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Formats.Tga;

internal static class TgaThrowHelper
{
    public static void ThrowInvalidImageContentException(string errorMessage)
        => throw new InvalidImageContentException(errorMessage);

    public static void ThrowInvalidImageContentException(string errorMessage, Exception innerException)
        => throw new InvalidImageContentException(errorMessage, innerException);

    public static void ThrowNotSupportedException(string errorMessage)
        => throw new NotSupportedException(errorMessage);
}

View on GitHub (pinned to 59ce6af6fc)