Skip to content

BMP with 1 bitcount (B/W) corrupts after save #2467

@synercoder

Description

@synercoder

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

3.0.1

Other ImageSharp packages and versions

>= 2.0.0 (tested up to 3.0.2-alpha.0.17)

Environment (Operating system, version and so on)

Ubuntu-latest & Windows-latest

.NET Framework version

7.0.0

Description

I have a bmp file that is 10x10 pixels. The bmp file has a bitcount of 1, and thus a color pallet of 2 colors, namely: FFFFFF & 000000.

When loading the bmp, the pixeldata is loaded correctly, but when saving the bmp, the image somehow becomes corrupt.

When you save the image by first copying the pixeldata into a new Image object, the resulting image is correct. Indicating that the bug has to do with the original metadata messing somehow with the encoding process.

The error bug occurs since ImageSharp version 2.0.0.

Input image:
Original image
original-large

Corrupt image:
Corrupt image
corrupt-large

Test project: https://github.com/synercoder/BitmapBug/blob/main/ImageSharp.BmpBug/BmpSavedWrong.cs
Test run showing matrix of os, mode & ImageSharp versions: https://github.com/synercoder/BitmapBug/actions/runs/5094509469

Steps to Reproduce

Use the attached image (datamatrix.bmp), and execute the following code:

using (var dmImage = Image.Load<Rgb24>("datamatrix.bmp"))
using (var fs = File.OpenWrite("corrupt.bmp")))
{
    dmImage.SaveAsBmp(fs);
}

The resulting corrupt.bmp is visibly (zoom in, it is 10x10 pixels) different on the right side of the image.

Images

datamatrix.zip

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