SixLabors/ImageSharp

SaveAsJpeg creates an image with a RED Background with latest ImageSharp

TonyValenti opened this issue · 8 comments

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.1 Does Not Work. 1.0.4 Does

Other ImageSharp packages and versions

See Above

Environment (Operating system, version and so on)

Win 11

.NET Framework version

.NET7

Description

This is a doozy!

When embedding certain JPEGs in a PDF, when using ImageSharp 1.0.4, the JPEGs render correctly.

However, when using ImageSharp 3.1.1, the JPEGs have a RED background instead of a white one.

Here is what the PDF looks like when using ImageSharp 1.0.4:
image

And here is what it looks like with ImageSharp 3.1.1:
image

I am using a slightly modified branch of PdfSharp which adds support for the latest build of ImageSharp.

The custom branch can be obtained from:
https://github.com/TonyValenti/PdfSharpCore

The branch is a stock branch except it adds support for legacy ImageSharp and modern ImageSharp verions.
The only differences between the build for ImageSharp 1.0.4 and 3.1.1 are these two files:

\PdfSharpCore\PdfSharpCore\Utils\ImageSharpImageSource_NET6.cs
\PdfSharpCore\PdfSharpCore\Utils\ImageSharpImageSource_NETSTANDARD.cs

which you will notice are identical except for a minor ImageSharp signature change.

Steps to Reproduce

Get these repo:

https://github.com/TonyValenti/ImageSharpRed  <== This demonstrates the issue
https://github.com/TonyValenti/PdfSharpCore

When you run ConsoleApp2 in ImageSharpRed, you can select the framework version:
image

When you select 5.0 (which is powered by ImageSharp 1.0.4) everything works correctly.
When you select 7.0 (which is powered by ImageSharp 3.1.1) this issue occurs.

Images

Here is a ZIP containing the image.
000809D7.zip

@TonyValenti We can only help you here, if you can provide a reproduction, which shows the issue without PdfSharpCore. We do not have anything to do with PdfSharpCore and cannot dig into their source code to find the issue.
If you cannot find a reproduction with only ImageSharp source code, it may be is better if you open an issue over at PdfSharpCore.

Hi @brianpopow -
I'm continuing to dig into this, comparing the output from v1.0.4 and v3.1.1
Variants.zip

I noticed that simply loading the image and resaving it is returning different results between the two versions:
Variants.zip

Specifically, the Bit Depth is different:
image

Given that v1 has a bit depth of 24, how do I make V3 do that?

I tried this:

        using var OriginalImage = SixLabors.ImageSharp.Image.Load(File);
        
        using var NewImage = OriginalImage.CloneAs<Rgb24>();

        NewImage.SaveAsJpeg($@"C:\Test - {Version}.jpg");

but the bit depth still comes out as 8 in 3.1.1.

v1 didn’t support 8bit encoding so defaulted to 24bit.

You should refer to the docs for encoding options. The pixel format represents the in-memory layout only not the encoded format. https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder.html?q=Jpegencoder

Thanks @JimBobSquarePants !
What settings would mimic the 1.0.4 defaults?

Actually, I think I found what I need.

Another question:
Is there a utility method anywhere that can allow me to convert Pixel Resolution Units?

For example, I'd like to convert 3780 Pixels per Meter to Pixels per Inch.

I’m assuming we can close this now?

Yes! Thank you so much for your help.