SixLabors/ImageSharp · error · InvalidImageContentException
(invalid memory operation)
Error message
{this.Dimensions} (invalid memory operation) What it means
During Identify, ImageDecoderCore catches InvalidMemoryOperationException thrown by decoders when the image header implies buffers/dimensions that violate memory bounds, and rethrows it as InvalidImageContentException carrying the image dimensions. It signals the file declares sizes or layout that cannot be safely allocated.
Solutions
- Catch InvalidImageContentException around Identify and treat the file as unidentifiable/corrupt
- Pre-check file size and magic bytes so obviously truncated files are rejected before decoding
- Re-obtain the file from a trusted source and verify its integrity
- If dimensions are the concern, validate MaxWidth/MaxHeight options or pre-read header bounds
Example fix
// before
var info = Image.Identify(stream); // InvalidImageContentException
// after
try { var info = Image.Identify(stream); }
catch (InvalidImageContentException ex) { logger.LogWarning(ex, "Corrupt image"); } Defensive patterns
Strategy: try-catch
Validate before calling
if (fileInfo.Length < 64) throw new InvalidOperationException("File too small to be a valid image."); Try / catch
try { var info = Image.Identify(stream); }
catch (InvalidImageContentException ex)
{
// header implies invalid memory operation — treat as corrupt
return Result.Corrupt;
} Prevention
- Reject files from untrusted sources or validate magic bytes first
- Catch InvalidImageContentException, not the internal InvalidMemoryOperationException
- Check for truncation when network transfers are involved
When it happens
Trigger: Calling Image.Identify/IdentifyAsync on a file whose header claims extreme dimensions, invalid component counts, or out-of-bounds offsets causing InvalidMemoryOperationException inside the format decoder.
Common situations: Corrupt or hostile images crafted to declare huge dimensions; truncated files where header fields are garbage; images produced by buggy encoders with inconsistent metadata.
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
- {message}
- Cannot read from the stream.
- Image cannot be loaded. Available decoders
- source
- This method is used for AOT code generation only. Do not…
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/1dec5d69616841c0.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Formats/ImageDecoderCore.cs:125
/// <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);
try
{
return this.Identify(bufferedReadStream, cancellationToken);
}
catch (InvalidMemoryOperationException ex)
{
throw new InvalidImageContentException(this.Dimensions, ex);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Decodes the image from the specified stream to an <see cref="Image{TPixel}" /> of a specific pixel type.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <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="Image{TPixel}" />.</returns>
/// <exception cref="InvalidImageContentException">Thrown if the encoded image contains errors.</exception>
public Image<TPixel> Decode<TPixel>(
Configuration configuration,View on GitHub (pinned to 59ce6af6fc)