SixLabors/ImageSharp · error · InvalidImageContentException

{message}

Error message

{message}

What it means

ImageDecoderCore.ThrowOrIgnoreNonStrictSegmentError converts malformed-segment reports into InvalidImageContentException when Options.SegmentIntegrityHandling is Strict. In non-strict modes the corrupt segment is tolerated and decoding continues, so this exception only surfaces for data that is structurally corrupt in a segment the caller asked to be strict about.

Solutions

  1. Set options.SegmentIntegrityHandling to SegmentIntegrityHandling.Ignore or DecodeMissingSegments/looser mode if tolerant decoding is acceptable
  2. Catch InvalidImageContentException and fall back to a placeholder image or error handling
  3. Repair or re-obtain the source file, verifying integrity (checksum, complete download)
  4. Validate the file is actually an image of the claimed format before decoding

Example fix

// before
var image = Image.Load(file); // InvalidImageContentException on corrupt segment
// after
var opts = new DecoderOptions { SegmentIntegrityHandling = SegmentIntegrityHandling.Ignore };
var image = Image.Load(opts, file);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!HasKnownImageMagicBytes(bytes)) throw new InvalidOperationException("Not a recognized image file.");

Try / catch

try { var image = Image.Load(opts, stream); }
catch (InvalidImageContentException ex)
{
    // strict segment handling rejected a corrupt segment
    logger.LogWarning(ex, "Corrupt image segment");
}

Prevention

When it happens

Trigger: Decoding/Identifying an image with a corrupted or non-conformant segment (e.g. truncated JPEG markers, bad chunk data) while options.SegmentIntegrityHandling == SegmentIntegrityHandling.Strict (the default on ImageDecoder options).

Common situations: Partially downloaded or truncated image files; images damaged by resaving/transcoding tools; deliberately fuzzed or malicious files; servers returning HTML error pages with image content types.

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/f2547f074c922b64. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/ImageDecoderCore.cs:100

            or InvalidImageContentException
            or InvalidOperationException
            or NotSupportedException)
        {
            // Intentionally ignored when image data integrity handling is set to IgnoreImageData.
        }
    }

    /// <summary>
    /// Throws unless the decoder is running in a non-strict segment integrity mode.
    /// Use this only from within <see cref="ExecuteAncillarySegmentAction"/> when local control flow
    /// must continue after the error.
    /// </summary>
    /// <param name="message">The exception message.</param>
    protected void ThrowOrIgnoreNonStrictSegmentError(string message)
    {
        if (this.Options.SegmentIntegrityHandling is SegmentIntegrityHandling.Strict)
        {
            throw new InvalidImageContentException(message);
        }
    }

    /// <summary>
    /// Reads the raw image information from the specified stream.
    /// </summary>
    /// <param name="configuration">The shared configuration.</param>
    /// <param name="stream">The <see cref="Stream" /> containing image data.</param>
    /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
    /// <returns>The <see cref="ImageInfo" />.</returns>
    /// <exception cref="InvalidImageContentException">Thrown if the encoded image contains errors.</exception>
    public ImageInfo Identify(
        Configuration configuration,
        Stream stream,
        CancellationToken cancellationToken)
    {
        using BufferedReadStream bufferedReadStream = new(configuration, stream, cancellationToken);

View on GitHub (pinned to 59ce6af6fc)