SixLabors/ImageSharp · error · ImageFormatException

Bad method for ZLIB header: cmf=

Error message

Bad method for ZLIB header: cmf={cmf}

What it means

RFC 1950 defines CM (compression method) only as 8 (deflate). When a critical chunk's ZLIB header has a different CMF value, InitializeInflateStream throws an ImageFormatException; non-critical chunks are silently skipped. This guards against streams the inflater cannot decode.

Solutions

  1. Verify the file is a valid PNG and its data chunks are intact (check CRC and header bytes 0x78).
  2. Re-download or re-export the image; treat the file as corrupted.
  3. Catch ImageFormatException and skip the malformed image.
Defensive patterns

Strategy: try-catch

Validate before calling

// Quick sniff: first byte of the zlib stream (start of IDAT data) should be 0x78.
bool zlibHeaderLooksValid = idatData[0] == 0x78;

Try / catch

try { using var img = Image.Load(stream); }
catch (ImageFormatException ex) when (ex.Message.Contains("Bad method for ZLIB"))
{ MarkCorrupt(stream); }

Prevention

When it happens

Trigger: Decoding a PNG (or reading a ZLIB stream) whose header byte's CM field is not 8 — e.g. a header corrupted in transit or produced by a non-ZLIB wrapper written into an IDAT chunk.

Common situations: Corrupt or deliberately malformed PNG files; data where the zlib header was overwritten; streams extracted at a wrong offset so the header bytes don't align.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Compression/Zlib/ZlibInflateReader.cs:95

        {
            // CINFO is the base-2 logarithm of the LZ77 window size, minus eight.
            int cinfo = (cmf & 0xF0) >> 4;

            if (cinfo > 7)
            {
                if (isCriticalChunk)
                {
                    // Values of CINFO above 7 are not allowed in RFC1950.
                    // CINFO is not defined in this specification for CM not equal to 8.
                    throw new ImageFormatException($"Invalid window size for ZLIB header: cinfo={cinfo}");
                }

                return false;
            }
        }
        else if (isCriticalChunk)
        {
            throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
        }
        else
        {
            return false;
        }

        // The preset dictionary.
        bool fdict = (flag & 32) != 0;
        if (fdict)
        {
            // We don't need this for inflate so simply skip by the next four bytes.
            // https://tools.ietf.org/html/rfc1950#page-6
            InlineArray4<byte> checksumBuffer = default;

            if (this.segmentStream.Read(checksumBuffer) != 4)
            {
                return false;
            }

View on GitHub (pinned to 59ce6af6fc)