Skip to content

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

@theknut

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

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