winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Saving JPEG as PNG using System.Drawing.Bitmap does not work on Windows 11 24H2

Open ollira opened this issue 1 year ago • 4 comments

.NET version

.NET 8.0

Did it work in .NET Framework?

Not tested/verified

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

Applications that read JPEG data and save as PNG using System.Drawing.Bitmap methods output incorrect PNG images on Windows 11 24H2. Output images contain only the top-left quarters of the respective source image content, enlarged 2x. No errors are reported.

Steps to reproduce

Sample app code is attached as a Visual Studio 2022 solution: WinFormsAppNet8.zip

using System.Drawing; using System.Drawing.Imaging;

            var bitmap1 = new Bitmap(jpgPath);
            var stream2 = new MemoryStream();
            bitmap1.Save(stream2, ImageFormat.Png);
            var bitmap2 = new Bitmap(stream2);

Result on Windows 11 24H2: bitmap2 contains only the top-left quarter of bitmap1 content, enlarged 2x.

ollira avatar Oct 16 '24 10:10 ollira

On 24H2 I'm seeing this:

Image

Investigating further.

Version details:

Edition Windows 11 Enterprise Version 24H2 Installed on ‎9/‎30/‎2024 OS build 26100.2033 Experience Windows Feature Experience Pack 1000.26100.23.0

JeremyKuhne avatar Oct 16 '24 17:10 JeremyKuhne

On 23H2: Image

Version details:

Edition Windows 11 Enterprise Version 23H2 Installed on ‎8/‎29/‎2024 OS build 22631.4317 Experience Windows Feature Experience Pack 1000.22700.1041.0

lonitra avatar Oct 16 '24 17:10 lonitra

+1 I have exactly the same behaviour since the 24H2 update (.NET Framework 4.8)

More details :

The problem concerns the following instruction : Save(String, ImageCodecInfo, EncoderParameters) https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.save?view=net-8.0

The same jpeg encoder is used and works on 23H2 but fails on 24H2. I can reproduce the problem with a jpeg input file and the EncoderParameter set to Imaging.Encoder.Quality (no matter the value specified)

It seems ok usingSave (string filename, System.Drawing.Imaging.ImageFormat format), but with a default quality.

florider06 avatar Oct 17 '24 12:10 florider06

This is a known issue in the WIC jpeg encoder (which GDI+ uses). The Windows team is aware, and it has been fixed. It should show up in servicing soon (next month or two most likely).

You can temporarily work around the problem by creating a copy of the original image and using that copy to save: Bitmap copy = new(originalImage);.

JeremyKuhne avatar Oct 21 '24 20:10 JeremyKuhne

@JeremyKuhne Is there something from MS that we could follow to know when this issue has been addressed?

andyshoover avatar Nov 01 '24 15:11 andyshoover

The problem looks fixed, can somebody from MS confirm?

AlexanderMagdysyuk avatar Feb 12 '25 06:02 AlexanderMagdysyuk

We are facing the same issue in .NET 9.0. When we upload an image in JPEG format using WinForms, it gets saved as PNG and updated in the .resx file, with the format changed unexpectedly. This issue only occurs in .NET 9.0.

We reviewed the .NET 9.0 release notes and noticed the introduction of GDI+ effects, which we suspect might be causing this issue due to limited support for JPEG. When we attempt to upload a JPEG image, we encounter the following exception:

System.Runtime.InteropServices.ExternalException HResult=0x80004005 Message=A generic error occurred in GDI+. Source=System.Private.Windows.Core

Please share if there is a solution or any workaround available to resolve this issue.

NagarajManimaran avatar Aug 07 '25 12:08 NagarajManimaran