SixLabors/ImageSharp · error · ImageFormatException

Bitmap does not have a valid format.

Error message

Bitmap does not have a valid format.

What it means

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.

Solutions

  1. Verify the file is a genuine, complete BMP (magic 'BM', plausible header sizes) before decoding.
  2. Re-export the image from the source application in a standard 24/32-bit BMP form.
  3. Catch ImageFormatException around Decode/Identify and fall back to sniffing the real format with Image.Identify.

Example fix

// before
using Image img = Image.Load(bytes);
// after
try { using Image img = Image.Load(bytes); }
catch (ImageFormatException ex) { /* inspect/convert the source file */ }
Defensive patterns

Strategy: try-catch

Validate before calling

if (bytes.Length < 2 || bytes[0] != (byte)'B' || bytes[1] != (byte)'M')
    throw new InvalidOperationException("Not a BMP file.");

Try / catch

try { using var img = Image.Load(path); }
catch (ImageFormatException ex) { /* verify/convert the source file */ }

Prevention

When it happens

Trigger: 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.

Common situations: Files renamed to .bmp that are actually another format; BMPs produced by buggy encoders with incorrect bfOffBits or biSizeImage; fuzzed/corrupt downloads.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs:220

                case BmpCompression.BitFields:
                case BmpCompression.BI_ALPHABITFIELDS:
                    this.ReadBitFields(stream, pixels, inverted);

                    break;

                default:
                    BmpThrowHelper.ThrowNotSupportedException("ImageSharp does not support this kind of bitmap files.");

                    break;
            }

            return image;
        }
        catch (IndexOutOfRangeException e)
        {
            image?.Dispose();
            throw new ImageFormatException("Bitmap does not have a valid format.", e);
        }
        catch
        {
            image?.Dispose();
            throw;
        }
    }

    /// <inheritdoc />
    protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
    {
        this.ReadImageHeaders(stream, out _, out _);
        return new ImageInfo(new Size(this.infoHeader.Width, this.infoHeader.Height), this.metadata);
    }

    /// <summary>
    /// Returns the y- value based on the given height.
    /// </summary>

View on GitHub (pinned to 59ce6af6fc)