SixLabors/ImageSharp · error · InvalidImageContentException

Not enough bytes to read icon header.

Error message

Not enough bytes to read icon header.

What it means

Thrown by CheckEndOfStream when a read of an icon structure (ICONDIR header or ICONDIRENTRY) returned fewer bytes than the required structure length, meaning the stream ended mid-header. SixLabors.ImageSharp throws InvalidImageContentException because the icon file is truncated and its directory cannot be read.

Solutions

  1. Check the file size is at least 6 + 16*Count bytes before loading
  2. Re-download or restore the complete icon file
  3. Catch InvalidImageContentException and treat the file as corrupt/empty
  4. If bundling icons, verify build packaging did not truncate them

Example fix

// before
var image = Image.Load("icon.ico");
// after
var fi = new FileInfo("icon.ico");
if (fi.Length < 6)
    throw new InvalidOperationException("File too small to be an ICO");
var image = Image.Load("icon.ico");
Defensive patterns

Strategy: validation

Validate before calling

static bool IcoHeaderIsComplete(string path)
{
    var fi = new FileInfo(path);
    if (fi.Length < 6) return false;
    using var fs = File.OpenRead(path);
    Span<byte> b = stackalloc byte[6];
    fs.ReadExactly(b);
    ushort count = (ushort)(b[4] | (b[5] << 8));
    return fi.Length >= 6 + 16L * count;
}

Try / catch

try
{
    var image = Image.Load(path);
}
catch (InvalidImageContentException)
{
    // stream ended inside the icon header: file truncated
    image = null;
}

Prevention

When it happens

Trigger: Calling Image.Load or Image.Identify on a file shorter than 6 bytes (no full ICONDIR) or whose stream ends before all 16-byte ICONDIRENTRY structures are read — e.g. a zero/one-byte file or an icon truncated inside the directory.

Common situations: Empty placeholder .ico files; interrupted downloads; files cut off at an exact block boundary; test fixtures accidentally committed empty.

Related errors


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

Appendix: source

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

            // Some established single-image ICO files overstate BytesInRes but contain a complete payload.
            // The containing stream is still a safe hard boundary because no sibling image can follow it.
            length = remaining;
        }

        frameStream.Reset(basePosition + entry.ImageOffset, length);
    }

    /// <summary>
    /// Ensures that a complete fixed-size directory structure was read.
    /// </summary>
    /// <param name="bytesRead">The number of bytes read.</param>
    /// <param name="expectedLength">The required structure length.</param>
    private static void CheckEndOfStream(int bytesRead, int expectedLength)
    {
        if (bytesRead != expectedLength)
        {
            throw new InvalidImageContentException("Not enough bytes to read icon header.");
        }
    }
}

View on GitHub (pinned to 59ce6af6fc)