SixLabors/ImageSharp · error · InvalidImageContentException

The embedded icon resource contains an invalid seek offset.

Error message

The embedded icon resource contains an invalid seek offset.

What it means

Thrown by IconFrameStream.Seek when the computed seek target is negative or beyond the embedded icon resource's Length; the single unsigned comparison rejects both cases. SixLabors.ImageSharp throws InvalidImageContentException because a seek outside the embedded child resource indicates corrupt directory metadata or a malformed payload stream.

Solutions

  1. Verify ImageOffset and BytesInRes of the entry are consistent with the file size
  2. Re-export the icon from a trusted source
  3. Catch InvalidImageContentException and treat the icon as corrupt
  4. If parsing icons from untrusted input, run validation first or sandbox the operation

Example fix

// before
var image = Image.Load("untrusted.ico");
// after
try
{
    var image = Image.Load("untrusted.ico");
}
catch (InvalidImageContentException ex)
{
    // invalid seek offset in embedded icon resource
    logger.LogWarning(ex, "Corrupt icon rejected");
    image = null;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// confirm the entry payload exists and is bounded before decoding
static bool EntryPayloadFits(ReadOnlySpan<byte> ico, int entryIndex)
{
    int e = 6 + 16 * entryIndex;
    uint offset = BitConverter.ToUInt32(ico.Slice(e + 12, 4));
    uint size = BitConverter.ToUInt32(ico.Slice(e + 8, 4));
    return offset + size <= (uint)ico.Length;
}

Type guard

static bool IsInvalidImageContent(Exception ex) => ex is InvalidImageContentException;

Try / catch

try
{
    var image = Image.Load(icoStream);
}
catch (InvalidImageContentException ex)
{
    // child decoder seeked outside the embedded resource: corrupt metadata
    LogCorruptIcon(icoStream, ex);
}

Prevention

When it happens

Trigger: Child decoder seeks (origin Begin/Current/End) computing a target outside [0, Length] of the embedded entry resource — caused by corrupt BytesInRes/ImageOffset metadata or a decoder reading inconsistent chunk sizes.

Common situations: Corrupt or fuzzed ICO/CUR files whose declared payload size does not match actual chunk structure; malformed BMP/PNG inner payloads that trigger out-of-range seeks during parsing.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Icon/IconFrameStream.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 icon resource contains an invalid seek offset.");
        }

        // Delay moving the containing stream until Read; this keeps logical seeks isolated from sibling resources.
        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)