SixLabors/ImageSharp · error · InvalidImageContentException

The image has an invalid size: width =

Error message

The image has an invalid size: width = {width}, height = {height}

What it means

QoiDecoderCore.ProcessHeader throws InvalidImageContentException with 'The image has an invalid size: width = {w}, height = {h}' when a QOI file's 14-byte header decodes to width or height 0 (big-endian uint32). A QOI image with a zero dimension has no pixels to decode, so the content is rejected. The message includes the decoded values for diagnosis.

Solutions

  1. Open the file and inspect bytes 4-12; if width/height are 0 the file is genuinely invalid — regenerate it from the source image with a working QOI encoder.
  2. Verify the file was fully written/transferred (check size against the producer's expected size).
  3. Catch InvalidImageContentException and report the file as an invalid QOI image.
  4. If you author QOI files, add encoder-side assertions that width and height are >= 1.

Example fix

// before
using var image = Image.Load("thumb.qoi"); // throws if header w/h == 0
// after
try
{
    using var image = Image.Load("thumb.qoi");
}
catch (InvalidImageContentException ex)
{
    Console.WriteLine($"Invalid QOI header: {ex.Message}");
}
Defensive patterns

Strategy: validation

Validate before calling

// Parse the QOI header manually before loading.
byte[] h = new byte[14];
using (var fs = File.OpenRead(path))
{
    if (fs.Read(h, 0, 14) != 14) throw new InvalidDataException("Not a complete QOI header.");
}
if (!h.AsSpan(0, 4).SequenceEqual("qoif"u8)) throw new InvalidDataException("Missing QOI magic.");
uint w = BinaryPrimitives.ReadUInt32BigEndian(h.AsSpan(4));
uint ht = BinaryPrimitives.ReadUInt32BigEndian(h.AsSpan(8));
if (w == 0 || ht == 0) throw new InvalidDataException($"Invalid QOI size: {w}x{ht}.");

Try / catch

// Complementary catch for files that slipped past validation.
try { using var img = Image.Load(path); }
catch (InvalidImageContentException ex) { Log(ex.Message); }

Prevention

When it happens

Trigger: Image.Load/Image.Identify on a .qoi file whose header bytes 5-12 encode width == 0 or height == 0; thrown from ProcessHeader, which is called by both Decode and Identify.

Common situations: QOI files produced by broken encoders; files zero-filled by a failed write or truncated download; hand-assembled test files; fuzzed inputs.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs:111

        // If it's a qoi image, read the rest of properties
        read = stream.Read(widthBytes);
        if (read != 4)
        {
            ThrowInvalidImageContentException();
        }

        read = stream.Read(heightBytes);
        if (read != 4)
        {
            ThrowInvalidImageContentException();
        }

        // These numbers are in Big Endian so we have to reverse them to get the real number
        uint width = BinaryPrimitives.ReadUInt32BigEndian(widthBytes);
        uint height = BinaryPrimitives.ReadUInt32BigEndian(heightBytes);
        if (width == 0 || height == 0)
        {
            throw new InvalidImageContentException(
                $"The image has an invalid size: width = {width}, height = {height}");
        }

        int channels = stream.ReadByte();
        if (channels is -1 or (not 3 and not 4))
        {
            ThrowInvalidImageContentException();
        }

        int colorSpace = stream.ReadByte();
        if (colorSpace is -1 or (not 0 and not 1))
        {
            ThrowInvalidImageContentException();
        }

        this.header = new QoiHeader(width, height, (QoiChannels)channels, (QoiColorSpace)colorSpace);
    }

View on GitHub (pinned to 59ce6af6fc)