Skip to content

ImageFormatException [0]: Bad method for ZLIB header: cmf=3 #2977

Description

@theknut

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.11

Other ImageSharp packages and versions

SixLabors.ImageSharp

Environment (Operating system, version and so on)

Windows 10

.NET Framework version

.NET Framework 4.8

Description

Sporadically when loading an image as byte array using SixLabors.ImageSharp.Image.Load an exception is thrown

ImageFormatException: Bad method for ZLIB header: cmf=3
     at bool SixLabors.ImageSharp.Compression.Zlib.ZlibInflateStream.InitializeInflateStream(bool isCriticalChunk)
     at void SixLabors.ImageSharp.Formats.Png.PngDecoderCore.ReadScanlines<TPixel>(PngChunk chunk, ImageFrame<TPixel> image, PngMetadata pngMetadata)
     at Image<TPixel> SixLabors.ImageSharp.Formats.Png.PngDecoderCore.Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
     at Image<TPixel> SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode<TPixel>(IImageDecoderInternals decoder, Configuration configuration, Stream stream, Func<InvalidMemoryOperationException, Size, InvalidImageContentException> largeImageExceptionFactory, CancellationToken cancellationToken) x 2
     at Image SixLabors.ImageSharp.Formats.Png.PngDecoder.Decode(Configuration configuration, Stream stream, CancellationToken cancellationToken)
     at (Image Image, IImageFormat Format) SixLabors.ImageSharp.Image.Decode(Stream stream, Configuration config, CancellationToken cancellationToken)
     at Image SixLabors.ImageSharp.Image.Load(Configuration configuration, Stream stream)+(Stream s) => {{ }}
     at T SixLabors.ImageSharp.Image.WithSeekableStream<T>(Configuration configuration, Stream stream, Func<Stream, T> action)
     at Image SixLabors.ImageSharp.Image.Load(Configuration configuration, Stream stream, out IImageFormat format) x 3

This issue is also described in #2924 but we are facing this issue in the 2.* lane of ImageSharp. Please apply fix of #2926 in 2.* IamgeSharp package.

As a workaround, when an ImageFormatException is thrown, we load the image bytes to a System.Drawing.Image first and load the bytes of the image to SixLabors.ImageSharp.Image.

using var byteToImageStream = new MemoryStream(imageBytes);
var systemDrawingImage = System.Drawing.Image.FromStream(byteToImageStream);
using var imageToBytesStream = new MemoryStream();
systemDrawingImage.Save(imageToBytesStream, ImageFormat.Png);
var image = SixLabors.ImageSharp.Image.Load(imageToBytesStream.ToArray());

Steps to Reproduce

Sporadic, no clear steps to reproduce other than
using var image = SixLabors.ImageSharp.Image.Load(imageBytes);

Images

No response

Activity

  1. JimBobSquarePants commented on Aug 20, 2025

    @JimBobSquarePants
    Member

    I have verified that I am running the latest version of ImageSharp

  2. theknut commented on Aug 21, 2025

    @theknut
    Author

    I had to check it since it was mandatory. I can not use 3.* because it no longer supports .NET Framework.

    Edit: There was a typo, I used 2.1.10 (not 2.1.1) because I thought the newly published package 2.1.11 caused this issue. So technically I used the latest version.

  3. TFTomSun commented on Aug 21, 2025

    @TFTomSun

    We run into the same issue with the latest 2.x version. We do imagine processing on images that are captured from the browser. It seems that a recent change in the chrome engine produces now sporadically images which are not compatible with that image sharp version. Is the .net framework support dropped? At least I saw that there were new 2.x versions published recently.

  4. JimBobSquarePants commented on Aug 21, 2025

    @JimBobSquarePants
    Member

    I only personally code and backport security updates for V2 since it is long (many years) obsolete. If someone wants a specific fix for it they must open a PR against the V2 release branch.

    Honestly I’m a little disappointed I’m expected to fix it given that a reference PR already exists.

  5. JimBobSquarePants commented on Aug 21, 2025

    @JimBobSquarePants
    Member

    So technically I used the latest version.

    @theknut The latest version is v3.1.11 a whole major version newer.

    The reason that field is mandatory is because I only support the latest version

  6. TFTomSun commented on Aug 22, 2025

    @TFTomSun

    Thanks for the clarification that the .NET framework compatible version is not getting bugfixes and features from your side anymore. We'll consider to create a PR or use a library that has full cross platform/cross framework support.

  7. mus65 commented on Sep 29, 2025

    @mus65

    The fix was backported to 2.1 with #2926 and just released with 2.1.12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions