SixLabors/ImageSharp · error · InvalidImageContentException

The icon directory header is invalid.

Error message

The icon directory header is invalid.

What it means

Thrown when the 6-byte ICONDIR header fails validation: the Reserved field is nonzero, the Type field does not match the expected icon type (1 = ICO, 2 = CUR), or the entry Count is zero. SixLabors.ImageSharp throws InvalidImageContentException because the file is not a valid icon resource.

Solutions

  1. Confirm the file is a real ICO (starts with 00 00 01 00) or CUR (00 00 02 00) and has at least one directory entry
  2. Re-export the icon from valid sources
  3. Catch InvalidImageContentException and fall back or report the file as corrupt
  4. Do not rename other image formats to .ico; let ImageSharp's format detection route them properly

Example fix

// before
var image = Image.Load("maybe.ico");
// after
byte[] head = File.ReadAllBytes("maybe.ico")[..4];
bool isIco = head[0] == 0 && head[1] == 0 && head[2] is 1 or 2 && head[3] == 0;
if (!isIco) throw new InvalidOperationException("Not an ICO/CUR file");
var image = Image.Load("maybe.ico");
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidIcoHeader(byte[] b) =>
    b.Length >= 6 && b[0] == 0 && b[1] == 0 && b[2] == 1 && b[3] == 0 && (b[4] | (b[5] << 8)) > 0;
// CUR uses b[2] == 2; ImageSharp picks the type from the detected format

Type guard

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

Try / catch

try { var image = Image.Load(path); }
catch (InvalidImageContentException ex)
{
    // invalid icon directory header
    throw new FormatException($"{path} is not a valid ICO/CUR", ex);
}

Prevention

When it happens

Trigger: Calling Image.Load or Image.Identify on a file routed to the IconDecoder whose ICONDIR header has Reserved != 0, a Type other than 1/2 (or mismatched with the detected CUR/ICO type), or Count == 0; also on truncated headers that fail the preceding end-of-stream check indirectly.

Common situations: Files with a wrong extension that get detected as icon; hand-written or corrupted headers; empty .ico files with zero entries; CUR vs ICO type confusion.

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

Appendix: source

Thrown at src/ImageSharp/Formats/Icon/IconDecoderCore.cs:298

        IconFrameCompression compression,
        BmpBitsPerPixel bitsPerPixel,
        ReadOnlyMemory<Color>? colorTable);

    /// <summary>
    /// Reads the icon directory entries needed by the configured frame limit.
    /// </summary>
    /// <param name="stream">The source stream.</param>
    [MemberNotNull(nameof(entries))]
    private void ReadHeader(Stream stream)
    {
        Span<byte> buffer = this.buffer;

        // ICONDIR
        CheckEndOfStream(stream.Read(buffer[..IconDir.Size]), IconDir.Size);
        this.fileHeader = IconDir.Parse(buffer);
        if (this.fileHeader.Reserved != 0 || this.fileHeader.Type != this.iconFileType || this.fileHeader.Count == 0)
        {
            throw new InvalidImageContentException("The icon directory header is invalid.");
        }

        // ICONDIRENTRY
        int entryCount = (int)Math.Min(this.fileHeader.Count, this.Options.MaxFrames);
        this.entries = new IconDirEntry[entryCount];
        for (int i = 0; i < this.entries.Length; i++)
        {
            CheckEndOfStream(stream.Read(buffer[..IconDirEntry.Size]), IconDirEntry.Size);
            this.entries[i] = IconDirEntry.Parse(buffer);
        }
    }

    /// <summary>
    /// Creates the decoder configured for an embedded PNG or headerless, double-height bitmap frame.
    /// </summary>
    /// <param name="isPng">Whether the embedded frame has a PNG signature.</param>
    /// <returns>The configured frame decoder.</returns>
    private ImageDecoderCore GetDecoder(bool isPng)

View on GitHub (pinned to 59ce6af6fc)