{"record":{"id":"850a2fc4bf7bf12c","repo":"SixLabors/ImageSharp","slug":"bitmap-does-not-have-a-valid-format","errorCode":null,"errorMessage":"Bitmap does not have a valid format.","messagePattern":"Bitmap does not have a valid format\\.","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs","lineNumber":220,"sourceCode":"\n                case BmpCompression.BitFields:\n                case BmpCompression.BI_ALPHABITFIELDS:\n                    this.ReadBitFields(stream, pixels, inverted);\n\n                    break;\n\n                default:\n                    BmpThrowHelper.ThrowNotSupportedException(\"ImageSharp does not support this kind of bitmap files.\");\n\n                    break;\n            }\n\n            return image;\n        }\n        catch (IndexOutOfRangeException e)\n        {\n            image?.Dispose();\n            throw new ImageFormatException(\"Bitmap does not have a valid format.\", e);\n        }\n        catch\n        {\n            image?.Dispose();\n            throw;\n        }\n    }\n\n    /// <inheritdoc />\n    protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)\n    {\n        this.ReadImageHeaders(stream, out _, out _);\n        return new ImageInfo(new Size(this.infoHeader.Width, this.infoHeader.Height), this.metadata);\n    }\n\n    /// <summary>\n    /// Returns the y- value based on the given height.\n    /// </summary>","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs#L202-L238","documentation":"The BMP decoder wraps unexpected IndexOutOfRangeException occurrences during Decode and rethrows them as ImageFormatException(\"Bitmap does not have a valid format.\"), disposing the partially built image. It signals that the pixel data or header layout implied by the file header does not match what the decoder's format tables expect — i.e. the file is corrupt or uses a layout the decoder didn't anticipate.","triggerScenarios":"Decoding a BMP whose bit-depth/width/row-size combination causes indexed access past a palette or scanline buffer; malformed or hostile BMP data (truncated file, bogus bpp/planes/clr-used fields) that slips past header validation.","commonSituations":"Files renamed to .bmp that are actually another format; BMPs produced by buggy encoders with incorrect bfOffBits or biSizeImage; fuzzed/corrupt downloads.","solutions":["Verify the file is a genuine, complete BMP (magic 'BM', plausible header sizes) before decoding.","Re-export the image from the source application in a standard 24/32-bit BMP form.","Catch ImageFormatException around Decode/Identify and fall back to sniffing the real format with Image.Identify."],"exampleFix":"// before\nusing Image img = Image.Load(bytes);\n// after\ntry { using Image img = Image.Load(bytes); }\ncatch (ImageFormatException ex) { /* inspect/convert the source file */ }","handlingStrategy":"try-catch","validationCode":"if (bytes.Length < 2 || bytes[0] != (byte)'B' || bytes[1] != (byte)'M')\n    throw new InvalidOperationException(\"Not a BMP file.\");","typeGuard":null,"tryCatchPattern":"try { using var img = Image.Load(path); }\ncatch (ImageFormatException ex) { /* verify/convert the source file */ }","preventionTips":["Verify the source file is a complete BMP, not a renamed file.","Re-export problem images in standard 24/32-bit BMP.","Use Image.Identify to sniff the true format on failure."],"tags":["bmp","decoding","corrupt-data","image-format"],"backgroundTag":"schema-validation-failed","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"}