SixLabors/ImageSharp · error · InvalidImageContentException

Invalid PNG data.

Error message

Invalid PNG data.

What it means

The parameterless variant is a catch-all for chunk-level data that is structurally invalid but not covered by a more specific message: wrong chunk length, illegal chunk ordering, bad internal layout, or other spec violations inside a chunk. ImageSharp surfaces it simply as "Invalid PNG data."

Solutions

  1. Locate the offending chunk with pngcheck and fix or re-export the image.
  2. Re-encode the image with a standard encoder (System.Drawing-free tools, ImageSharp itself, pnglib).
  3. If you write PNGs programmatically, validate chunk layout against the PNG specification.
  4. Catch InvalidImageContentException for untrusted-input handling.
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex) when (ex.Message == "Invalid PNG data.")
{
    // generic chunk-structure violation — treat input as corrupt
}

Prevention

When it happens

Trigger: Decoding any PNG where a chunk's contents or placement violate the PNG spec in a way the decoder detects generically — e.g. invalid chunk sequence, malformed ancillary chunk payload, impossible interlace or filter metadata combinations.

Common situations: Hand-crafted or mutated PNGs, fuzzed test files, images produced by low-quality encoders, files corrupted in storage or transit.

Related errors


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

Appendix: source

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

    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}'.");

    [DoesNotReturn]
    public static void ThrowNotSupportedColor() => throw new NotSupportedException("Unsupported PNG color type.");

View on GitHub (pinned to 59ce6af6fc)