SixLabors/ImageSharp · error · InvalidImageContentException

PNG Image does not contain a palette chunk.

Error message

PNG Image does not contain a palette chunk.

What it means

Thrown when decoding an indexed-color (palette-based) PNG whose PLTE (palette) chunk is absent. Palette and grayscale+palette color types are defined by their palette; without PLTE the pixel indices cannot be mapped to colors, so the decode is invalid.

Solutions

  1. Re-export the image ensuring a PLTE chunk is written for indexed color type 3.
  2. Re-save as RGB/RGBA (non-palette) if a palette is not required.
  3. Check the file wasn't truncated between IHDR and PLTE.
  4. Catch InvalidImageContentException and reject the input.

Example fix

// before (encoder)
WriteChunk(ihdr with colorType=Indexed); WriteChunk(idat);
// after
WriteChunk(ihdr with colorType=Indexed); WriteChunk(plte); WriteChunk(idat);
Defensive patterns

Strategy: validation

Validate before calling

// For indexed PNGs (color type 3), confirm a PLTE chunk exists before IDAT by scanning chunk headers
static bool HasPlte(ReadOnlySpan<byte> png) { /* walk chunks: len, type, data, crc */ return foundPlte; }

Try / catch

try
{
    using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex) when (ex.Message.Contains("palette"))
{
    // indexed PNG without PLTE — reject or re-encode as RGBA
}

Prevention

When it happens

Trigger: Decoding a PNG with color type 3 (indexed) that contains no PLTE chunk before the IDAT data; truncated files that lost the PLTE; files mislabeled with an indexed color type.

Common situations: Bit-reduced or quantized images saved by buggy tools without the palette, files edited to change the color type but not add a palette, truncated downloads.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Png/PngThrowHelper.cs:30

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

    [DoesNotReturn]
    public static void ThrowInvalidHeader() => throw new InvalidImageContentException("PNG Image must contain a header chunk and it must be located before any other chunks.");

    [DoesNotReturn]
    public static void ThrowNoData() => throw new InvalidImageContentException("PNG Image does not contain a data chunk.");

    [DoesNotReturn]
    public static void ThrowMissingDefaultData() => throw new InvalidImageContentException("APNG Image does not contain a default data chunk.");

    [DoesNotReturn]
    public static void ThrowInvalidAnimationControl() => throw new InvalidImageContentException("APNG Image must contain a acTL chunk and it must be located before any IDAT and fdAT chunks.");

    [DoesNotReturn]
    public static void ThrowMissingFrameControl() => throw new InvalidImageContentException("One of APNG Image's frames do not have a frame control chunk.");

    [DoesNotReturn]
    public static void ThrowMissingPalette() => throw new InvalidImageContentException("PNG Image does not contain a palette chunk.");

    [DoesNotReturn]
    public static void ThrowInvalidChunkType() => throw new InvalidImageContentException("Invalid PNG data.");

    [DoesNotReturn]
    public static void ThrowInvalidChunkType(string message) => throw new InvalidImageContentException(message);

    [DoesNotReturn]
    public static void ThrowInvalidChunkCrc(string chunkTypeName) => throw new InvalidImageContentException($"CRC Error. PNG {chunkTypeName} chunk is corrupt!");

    [DoesNotReturn]
    public static void ThrowInvalidParameter(object value, string message, [CallerArgumentExpression(nameof(value))] string name = "")
        => throw new NotSupportedException($"Invalid {name}. {message}. Was '{value}'.");

    [DoesNotReturn]
    public static void ThrowInvalidParameter(object value1, object value2, string message, [CallerArgumentExpression(nameof(value1))] string name1 = "", [CallerArgumentExpression(nameof(value2))] string name2 = "")
        => throw new NotSupportedException($"Invalid {name1} or {name2}. {message}. Was '{value1}' and '{value2}'.");

View on GitHub (pinned to 59ce6af6fc)