SixLabors/ImageSharp · error · InvalidImageContentException

{message}

Error message

{message}

What it means

The string-overload of ThrowInvalidChunkType lets individual decoder call sites raise InvalidImageContentException with a chunk-specific message describing exactly which part of the PNG data was invalid. The thrown message is passed through verbatim, so it pinpoints the failing structure.

Solutions

  1. Read the exception message to identify which chunk/field is invalid, then correct it in the source file.
  2. Re-export the image from the original source with a standard encoder.
  3. Validate the file with pngcheck to get a spec-level diagnosis.
  4. Catch InvalidImageContentException to gracefully reject malformed uploads.

Example fix

// before
var ihdr = new IhdrChunk { BitDepth = 3, ColorType = PngColorType.Rgb };
// after
var ihdr = new IhdrChunk { BitDepth = 8, ColorType = PngColorType.Rgb }; // valid combination
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
    // ex.Message pinpoints the invalid chunk/field — log it for diagnosis
    logger.LogWarning("Invalid PNG chunk: {Message}", ex.Message);
}

Prevention

When it happens

Trigger: Decoding a PNG where a specific chunk fails detailed validation and the decoder supplies a tailored message, e.g. bad bit-depth/color-type combination in IHDR, invalid interlace flag, or malformed chunk-specific payloads.

Common situations: Files with IHDR fields outside the allowed combinations (e.g. bit depth 3), corrupt ancillary chunks, images generated by non-conformant encoders, deliberately mutated test files.

Related errors


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

Appendix: source

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

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

    [DoesNotReturn]
    public static void ThrowUnknownFilter() => throw new InvalidImageContentException("Unknown filter type.");
}

View on GitHub (pinned to 59ce6af6fc)