SixLabors/ImageSharp · error · InvalidImageContentException

Not enough bytes to read the

Error message

Not enough bytes to read the {description}.

What it means

Thrown by AniDecoderCore.ReadExactly when a bounded stream read returns fewer bytes than the destination span length. Used for all fixed-size ANI reads (headers, type fields, etc.); the {description} placeholder names what could not be read (e.g. 'RIFF list type', 'ANI header'). It signals the stream ended mid-structure.

Solutions

  1. Re-obtain a complete ANI file; verify size against the source.
  2. Check the stream source (file/network) is fully available and not cut off before decoding.
  3. Catch InvalidImageContentException and surface the description from the message to pinpoint the missing structure.

Example fix

// before
var image = Image.Load(stream);
// after
if (stream.CanSeek && stream.Length < 64) throw new InvalidDataException("ANI stream too short");
var image = Image.Load(stream);
Defensive patterns

Strategy: try-catch

Validate before calling

if (stream.CanSeek && stream.Length < 6) throw new InvalidDataException("Stream too short to be an ANI file.");

Try / catch

try { var image = await Image.LoadAsync(stream); } catch (InvalidImageContentException ex) { log.Error($"Short read in ANI: {ex.Message}"); }

Prevention

When it happens

Trigger: Decoding or identifying an ANI file where any fixed-size structure read (RIFF chunk headers, ANI header fields, icon directory entries) hits end-of-stream before the full span is filled.

Common situations: Truncated downloads, files cut off mid-header, corrupted ANI files, streams from network sources that ended prematurely.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Ani/AniDecoderCore.cs:852

        if (paddedEnd > containerEnd)
        {
            throw new InvalidImageContentException("An ANI RIFF chunk is missing its alignment padding.");
        }

        return paddedEnd;
    }

    /// <summary>
    /// Reads an exact number of bytes or reports a truncated ANI file.
    /// </summary>
    /// <param name="stream">The ANI stream.</param>
    /// <param name="destination">The destination buffer.</param>
    /// <param name="description">The data description used in the error message.</param>
    private static void ReadExactly(BufferedReadStream stream, Span<byte> destination, string description)
    {
        if (stream.Read(destination) != destination.Length)
        {
            throw new InvalidImageContentException($"Not enough bytes to read the {description}.");
        }
    }

    /// <summary>
    /// Converts a pixel dimension to the one-byte ICO/CUR representation.
    /// </summary>
    /// <param name="value">The pixel dimension.</param>
    /// <returns>The encoded dimension, where zero represents 256 pixels or greater.</returns>
    private static byte NarrowDimension(int value) => value > byte.MaxValue ? (byte)0 : (byte)value;
}

View on GitHub (pinned to 59ce6af6fc)