SixLabors/ImageSharp · error · InvalidImageContentException
{errorMessage}
Error message
{errorMessage} What it means
This is the generic PNG content-error entry point in PngThrowHelper. SixLabors.ImageSharp throws InvalidImageContentException when the PNG stream being decoded violates the PNG specification in a way not covered by a more specific helper, so the decoder aborts rather than return a corrupt image. The message is supplied by the caller, so read the exception message for the exact problem.
Solutions
- Read the exception message to identify the specific PNG violation, then fix or regenerate the source image.
- Verify the file is a genuine PNG (starts with the 8-byte PNG signature) and is not truncated.
- Validate the image with an external tool (e.g. pngcheck) before loading.
- Wrap the load call in a try-catch for InvalidImageContentException and handle untrusted/corrupt input gracefully.
Example fix
// before
using var image = Image.Load(stream);
// after
try
{
using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
// log ex.Message and treat input as corrupt
} Defensive patterns
Strategy: try-catch
Validate before calling
if (span.Length < 8 || !span.Slice(0, 8).SequenceEqual(new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }))
throw new InvalidOperationException("Not a PNG file"); Type guard
static bool IsPng(Stream s)
{
Span<byte> sig = stackalloc byte[8];
return s.Read(sig) == 8 && sig.SequenceEqual(stackalloc byte[] { 137, 80, 78, 71, 13, 10, 26, 10 });
} Try / catch
try
{
using var image = Image.Load(stream);
}
catch (InvalidImageContentException ex)
{
logger.LogWarning(ex, "Rejecting corrupt PNG input");
} Prevention
- Check the PNG signature before decoding untrusted input.
- Run pngcheck on files that come from external sources.
- Use checksums/hashes when transferring images over the network.
When it happens
Trigger: Any call to Image.Load/LoadAsync, Image.Load<TPixel>, or decoding via PngDecoder where a chunk fails validation and the decoder calls ThrowInvalidImageContentException(errorMessage) with a decoder-specific message.
Common situations: Feeding truncated or hand-edited PNG files, streams containing non-PNG data renamed to .png, files corrupted in transfer, or re-encoded images with spec-violating chunk contents.
Related errors
- Invalid PNG data.
- PNG Image must contain a header chunk and it must be…
- PNG Image does not contain a data chunk.
- {message}
- CRC Error. PNG chunk is corrupt!
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/0ae33aff70a41ebb.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/Png/PngThrowHelper.cs:12
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Formats.Png;
internal static class PngThrowHelper
{
[DoesNotReturn]
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.");View on GitHub (pinned to 59ce6af6fc)