SixLabors/ImageSharp · error · InvalidImageContentException

The embedded ANI frame resource contains an invalid seek…

Error message

The embedded ANI frame resource contains an invalid seek offset.

What it means

AniFrameStream is a read-only view over one embedded ANI frame resource. Seek validates the computed target against the resource length with a single unsigned bounds check; a negative offset or a position past the end throws InvalidImageContentException, indicating the resource bytes or a caller-computed offset are invalid.

Solutions

  1. Clamp or validate offsets against stream.Length before calling Seek.
  2. Re-extract the ANI file; the embedded resource sizes may be corrupt.
  3. Use SeekOrigin.Begin with absolute offsets computed from the resource start.

Example fix

// before
frameStream.Seek(frameStream.Position + advance, SeekOrigin.Current); // may overrun
// after
long target = frameStream.Position + advance;
if (target >= 0 && target <= frameStream.Length) frameStream.Seek(target, SeekOrigin.Begin);
Defensive patterns

Strategy: validation

Validate before calling

long target = frameStream.Position + advance;
if (target < 0 || target > frameStream.Length) throw new InvalidOperationException("Seek target out of resource bounds.");

Try / catch

try { frameStream.Seek(offset, SeekOrigin.Begin); }
catch (InvalidImageContentException) { /* resource offsets/sizes are corrupt; re-extract file */ }

Prevention

When it happens

Trigger: Calling Seek on the frame stream with an origin/offset combination yielding target < 0 or target > Length — e.g. Seek(offset, SeekOrigin.Current) with an offset that overruns the resource, or corrupt RIFF 'data' chunk sizes making Length smaller than expected.

Common situations: Parsing hand-built or truncated ANI files where chunk sizes don't match actual bytes; third-party code positioning the stream with stale offsets after the resource was replaced with a shorter one.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Ani/AniFrameStream.cs:101

        return read;
    }

    /// <inheritdoc/>
    public override long Seek(long offset, SeekOrigin origin)
    {
        long target = origin switch
        {
            SeekOrigin.Begin => offset,
            SeekOrigin.Current => this.position + offset,
            SeekOrigin.End => this.length + offset,
            _ => throw new ArgumentOutOfRangeException(nameof(origin))
        };

        // Casting rejects both negative offsets and offsets beyond Length with one bounds check.
        if ((ulong)target > (ulong)this.length)
        {
            throw new InvalidImageContentException("The embedded ANI frame resource contains an invalid seek offset.");
        }

        // Delay moving the containing stream until Read; this keeps logical seeks isolated from sibling resource processing.
        this.position = target;
        return target;
    }

    /// <inheritdoc/>
    public override void SetLength(long value) => throw new NotSupportedException();

    /// <inheritdoc/>
    public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}

View on GitHub (pinned to 59ce6af6fc)