{"record":{"id":"ee7a673da86f0c7e","repo":"SixLabors/ImageSharp","slug":"the-image-has-an-invalid-size-width-width-height-height","errorCode":null,"errorMessage":"The image has an invalid size: width = {width}, height = {height}","messagePattern":"The image has an invalid size: width = (.+?), height = (.+?)","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs","lineNumber":111,"sourceCode":"        // If it's a qoi image, read the rest of properties\n        read = stream.Read(widthBytes);\n        if (read != 4)\n        {\n            ThrowInvalidImageContentException();\n        }\n\n        read = stream.Read(heightBytes);\n        if (read != 4)\n        {\n            ThrowInvalidImageContentException();\n        }\n\n        // These numbers are in Big Endian so we have to reverse them to get the real number\n        uint width = BinaryPrimitives.ReadUInt32BigEndian(widthBytes);\n        uint height = BinaryPrimitives.ReadUInt32BigEndian(heightBytes);\n        if (width == 0 || height == 0)\n        {\n            throw new InvalidImageContentException(\n                $\"The image has an invalid size: width = {width}, height = {height}\");\n        }\n\n        int channels = stream.ReadByte();\n        if (channels is -1 or (not 3 and not 4))\n        {\n            ThrowInvalidImageContentException();\n        }\n\n        int colorSpace = stream.ReadByte();\n        if (colorSpace is -1 or (not 0 and not 1))\n        {\n            ThrowInvalidImageContentException();\n        }\n\n        this.header = new QoiHeader(width, height, (QoiChannels)channels, (QoiColorSpace)colorSpace);\n    }\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Qoi/QoiDecoderCore.cs#L93-L129","documentation":"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.","triggerScenarios":"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.","commonSituations":"QOI files produced by broken encoders; files zero-filled by a failed write or truncated download; hand-assembled test files; fuzzed inputs.","solutions":["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.","Verify the file was fully written/transferred (check size against the producer's expected size).","Catch InvalidImageContentException and report the file as an invalid QOI image.","If you author QOI files, add encoder-side assertions that width and height are >= 1."],"exampleFix":"// before\nusing var image = Image.Load(\"thumb.qoi\"); // throws if header w/h == 0\n// after\ntry\n{\n    using var image = Image.Load(\"thumb.qoi\");\n}\ncatch (InvalidImageContentException ex)\n{\n    Console.WriteLine($\"Invalid QOI header: {ex.Message}\");\n}","handlingStrategy":"validation","validationCode":"// Parse the QOI header manually before loading.\nbyte[] h = new byte[14];\nusing (var fs = File.OpenRead(path))\n{\n    if (fs.Read(h, 0, 14) != 14) throw new InvalidDataException(\"Not a complete QOI header.\");\n}\nif (!h.AsSpan(0, 4).SequenceEqual(\"qoif\"u8)) throw new InvalidDataException(\"Missing QOI magic.\");\nuint w = BinaryPrimitives.ReadUInt32BigEndian(h.AsSpan(4));\nuint ht = BinaryPrimitives.ReadUInt32BigEndian(h.AsSpan(8));\nif (w == 0 || ht == 0) throw new InvalidDataException($\"Invalid QOI size: {w}x{ht}.\");","typeGuard":null,"tryCatchPattern":"// Complementary catch for files that slipped past validation.\ntry { using var img = Image.Load(path); }\ncatch (InvalidImageContentException ex) { Log(ex.Message); }","preventionTips":["Validate QOI magic bytes and non-zero dimensions before decode.","Check QOI files end with the 8-byte end marker.","Regenerate QOI files from source images with a maintained encoder.","Never trust zero-dimension headers from untrusted sources."],"tags":["qoi","image-decoding","invalid-header","zero-dimension"],"backgroundTag":"value-out-of-range","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}