SixLabors/ImageSharp · error · InvalidImageContentException

An ANI RIFF chunk is missing its alignment padding.

Error message

An ANI RIFF chunk is missing its alignment padding.

What it means

Thrown by AniDecoderCore.GetPaddedEnd when the chunk's 16-bit-aligned padded end (dataEnd plus one pad byte for odd sizes) would exceed the container end. RIFF pads odd-sized chunks to even boundaries; the library requires that pad byte to exist inside the list. Missing padding means the file is structurally incomplete.

Solutions

  1. Re-export the ANI with a compliant writer that emits RIFF pad bytes for odd-sized chunks.
  2. Append the missing pad byte if editing the file manually.
  3. Catch InvalidImageContentException and treat the file as structurally invalid.
Defensive patterns

Strategy: validation

Validate before calling

// For each odd-sized chunk, verify one pad byte exists before the next chunk header.
bool hasPadByte = chunkDataEnd + 1 <= containerEnd;

Try / catch

try { var image = Image.Load(path); } catch (InvalidImageContentException ex) when (ex.Message.Contains("alignment padding")) { /* writer omitted pad byte */ }

Prevention

When it happens

Trigger: Decoding an ANI file where an odd-sized chunk sits at the very end of its containing list with no alignment pad byte, so dataEnd + (size & 1) > containerEnd.

Common situations: Files truncated exactly at a chunk boundary (pad byte stripped), ANI files written by tools that omit RIFF padding, hand-edited files.

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

Appendix: source

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

        }

        return end;
    }

    /// <summary>
    /// Calculates and validates the word-aligned end of a RIFF chunk.
    /// </summary>
    /// <param name="dataEnd">The exclusive payload boundary.</param>
    /// <param name="size">The declared payload size.</param>
    /// <param name="containerEnd">The exclusive parent-container boundary.</param>
    /// <returns>The exclusive padded chunk boundary.</returns>
    private static long GetPaddedEnd(long dataEnd, uint size, long containerEnd)
    {
        // RIFF aligns each chunk to a 16-bit boundary without including the optional pad byte in the declared size.
        long paddedEnd = dataEnd + (size & 1);
        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}.");
        }
    }

View on GitHub (pinned to 59ce6af6fc)