{"record":{"id":"f8fcbcd517036cff","repo":"SixLabors/ImageSharp","slug":"invalid-or-unsupported-bit-depth-was-this-bitdepth","errorCode":null,"errorMessage":"Invalid or unsupported bit depth. Was '{this.BitDepth}'.","messagePattern":"Invalid or unsupported bit depth\\. Was '(.+?)'\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Png/Chunks/PngHeader.cs","lineNumber":98,"sourceCode":"    /// </summary>\n    public PngInterlaceMode InterlaceMethod { get; }\n\n    /// <summary>\n    /// Validates the png header.\n    /// </summary>\n    /// <exception cref=\"NotSupportedException\">\n    /// Thrown if the image does pass validation.\n    /// </exception>\n    public void Validate()\n    {\n        if (!PngConstants.ColorTypes.TryGetValue(this.ColorType, out byte[] supportedBitDepths))\n        {\n            throw new NotSupportedException($\"Invalid or unsupported color type. Was '{this.ColorType}'.\");\n        }\n\n        if (supportedBitDepths.AsSpan().IndexOf(this.BitDepth) == -1)\n        {\n            throw new NotSupportedException($\"Invalid or unsupported bit depth. Was '{this.BitDepth}'.\");\n        }\n\n        if (this.FilterMethod != 0)\n        {\n            throw new NotSupportedException($\"Invalid filter method. Expected 0. Was '{this.FilterMethod}'.\");\n        }\n\n        // The png specification only defines 'None' and 'Adam7' as interlaced methods.\n        if (this.InterlaceMethod is not PngInterlaceMode.None and not PngInterlaceMode.Adam7)\n        {\n            throw new NotSupportedException($\"Invalid interlace method. Expected 'None' or 'Adam7'. Was '{this.InterlaceMethod}'.\");\n        }\n    }\n\n    /// <summary>\n    /// Writes the header to the given buffer.\n    /// </summary>\n    /// <param name=\"buffer\">The buffer to write to.</param>","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Png/Chunks/PngHeader.cs#L80-L116","documentation":"PngHeader.Validate throws this NotSupportedException when the IHDR bit depth is not among the depths the PNG specification allows for the chunk's color type (e.g. only 1/2/4/8/16 for grayscale, only 8/16 for RGB). The color type lookup succeeded, but the paired BitDepth value is not in PngConstants.ColorTypes[ColorType], so the image cannot be decoded.","triggerScenarios":"Decoding a PNG whose IHDR combines a legal color type with an illegal bit depth — for example color type 3 (palette) with bit depth 16, or a fuzzed/corrupted bit-depth byte.","commonSituations":"Files produced by non-conformant encoders, hand-crafted or fuzzed PNGs, corruption during transfer, or converting images with a tool that sets bit depth independently of color type.","solutions":["Re-save the image with a standard tool (viewers, ImageSharp) so encoder picks a valid bit depth/color type combination.","Verify the file integrity and re-download if the source may be corrupted.","Catch NotSupportedException around the load call and handle the invalid file gracefully.","If a custom encoder produced the file, fix it to emit only depths valid for the chosen color type."],"exampleFix":"// before\nusing var image = Image.Load(\"16bit-palette.png\"); // palette + 16-bit is illegal\n// after\ntry\n{\n    using var image = Image.Load(\"16bit-palette.png\");\n}\ncatch (NotSupportedException)\n{\n    using var image = Image.Load(\"16bit-palette-converted.png\"); // re-exported as 8-bit RGB\n}","handlingStrategy":"try-catch","validationCode":"// valid bit depths per color type per PNG spec\nstatic readonly Dictionary<byte, byte[]> Valid = new()\n{\n    [0] = new byte[] { 1, 2, 4, 8, 16 },\n    [2] = new byte[] { 8, 16 },\n    [3] = new byte[] { 1, 2, 4, 8 },\n    [4] = new byte[] { 8, 16 },\n    [6] = new byte[] { 8, 16 }\n};\n// check: Valid[colorType].Contains(bitDepth)","typeGuard":"static bool IsValidBitDepthForColorType(byte colorType, byte bitDepth) =>\n    colorType switch\n    {\n        0 => bitDepth is 1 or 2 or 4 or 8 or 16,\n        2 or 4 or 6 => bitDepth is 8 or 16,\n        3 => bitDepth is 1 or 2 or 4 or 8,\n        _ => false\n    };","tryCatchPattern":"try\n{\n    using var image = Image.Load(pngStream);\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"bit depth\"))\n{\n    using var repaired = ReExportWithStandardEncoder(originalPath); // re-save via another tool\n}","preventionTips":["Never construct PNG encoder options mixing palette color type with 16-bit depth.","When transcoding, let the encoder pick the bit depth instead of copying source values.","Validate third-party-produced PNGs before ingesting them into pipelines."],"tags":["png","ihdr","bit-depth","invalid-header"],"backgroundTag":"unsupported-enum-value","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"}