Skip to content

PngEncoder image size increases by 200% to 300% when specifying ColorType #3041

@MeltyPlayer

Description

@MeltyPlayer

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.1.12

Other ImageSharp packages and versions

3.1.11

Environment (Operating system, version and so on)

Windows 11

.NET Framework version

.NET 10, C# 14

Description

I was trying to cut down on the size of the PNGs I was writing, so I tried looking through the PngEncoder properties to see if there were any hints I could give it as an optimization.

I noticed that PngEncoder.ColorType could be set to Rgb/RgbWithAlpha/Grayscale/GrayscaleWithAlpha, so it seemed plausible this was such a hint? (It's kind of hard to tell what PngEncoder.ColorType is meant to be, since its documentation isn't very helpful--it's just described as "the color type", which I had already inferred from the name.)

When I actually set this property, though, I find that the PNGs I write actually increase in size by anywhere from 200% to 300% (compared to when written without setting the property).

I wanted to verify if this was intended behavior, and also to ask if you could improve the documentation for this field so it's clearer how it's meant to be used?

Steps to Reproduce

  1. Load one of the following images into C# via ImageSharp, it should be an Image<Rgb24>.
  2. Save it via the following two methods:
image.Save(
  stream,
  new PngEncoder {
      SkipMetadata = true,
  });
image.Save(
  stream,
  new PngEncoder {
      SkipMetadata = true,
      ColorType = PngColorType.Rgb
  });

The image without ColorType set will be smaller than the input image, but the image with ColorType set to PngColorType.Rgb will be significantly larger.

Images

textures.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