Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageCodecInfo.GetImageEncoders() throws fatal exception in 9.0.0 package. #12494

Closed
bitbound opened this issue Nov 15, 2024 · 8 comments · Fixed by #12501
Closed

ImageCodecInfo.GetImageEncoders() throws fatal exception in 9.0.0 package. #12494

bitbound opened this issue Nov 15, 2024 · 8 comments · Fixed by #12501
Assignees
Labels
🪲 bug Product bug (most likely) 💥 regression-release Regression from a public release 🚧 work in progress Work that is current in progress

Comments

@bitbound
Copy link

.NET version

9.0

Did it work in .NET Framework?

Yes

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

Yes. It worked in 8.0.x.

Issue description

ImageCodecInfo.GetImageEncoders() throws a fatal exception in the 9.0.0 version of System.Drawing.Common.

Steps to reproduce

Exception in 9.0

Image

Working in 8.0

Image

@bitbound bitbound added the untriaged The team needs to look at this issue in the next triage label Nov 15, 2024
@elachlan elachlan added 🪲 bug Product bug (most likely) 💥 regression-release Regression from a public release labels Nov 15, 2024
@JeremyKuhne JeremyKuhne self-assigned this Nov 17, 2024
@JeremyKuhne
Copy link
Member

@bitbound you can work around this by getting one of the Brushes or Pens, etc.

Validation that GDI+ is initialized slipped through in this use case. Need to check other places PInvokes come from the Core assembly.

Will fix and put up for servicing.

@bitbound
Copy link
Author

bitbound commented Nov 17, 2024

@JeremyKuhne Thanks for the reply. I'm not sure what you mean about Brushes/Pens, though. I'm not seeing how I'd get an ImageCodecInfo out of one of them. Nevermind. I understand what you mean now about the initialization.

If it helps, here's my use case. This is in a desktop console app (Windows only). Is there another way to adjust encoding quality that'd work around this issue? Or would you recommend migrating to SkiaSharp?

  private readonly ImageCodecInfo _jpegEncoder = ImageCodecInfo
    .GetImageEncoders()
    .First(x => x.FormatID == ImageFormat.Jpeg.Guid);

  public byte[] EncodeJpeg(Bitmap bitmap, int quality)
  {
    using var ms = new MemoryStream();
    using var encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);
    bitmap.Save(ms, _jpegEncoder, encoderParams);
    return ms.GetBuffer();
  }

@JeremyKuhne
Copy link
Member

@bitbound presumably you're ok.

Since I wasn't super clear here, you can just access a static resource to work around this until we get a servicing fix. So _ = Pens.Black; or something like that before calling the affected methods. There is one other path I missed, but that is less likely to be hit (getting the halftone palette).

@dotnet-policy-service dotnet-policy-service bot removed the untriaged The team needs to look at this issue in the next triage label Nov 18, 2024
@JeremyKuhne JeremyKuhne reopened this Nov 18, 2024
@JeremyKuhne
Copy link
Member

Need to service .NET 9.

@JeremyKuhne
Copy link
Member

Actually, the only method I missed in .NET 9 was GetImageEncoders.

@Philip-Wang01
Copy link
Contributor

Verified this issue in the 9.0.0 rc2 build + release/9.0 private dll, it was fixed now. If you call GetImageEncoders before any other System.Drawing code, no error occurs.

Before the fix:
Image

After the fix, now:
Image

@John-Qiao
Copy link
Member

John-Qiao commented Nov 21, 2024

@JeremyKuhne Verified the issue on latest .NET SDK 10.0.100-alpha.1.24570.21, there's no exception in ImageCodecInfo.GetImageEncoders() now. But the Non-Public members node does not show in each GetImageEncoders returned item in .NET 10.0, is that the expected result?

.NET 10.0:
Image

.NET 8.0:
Image

@JeremyKuhne
Copy link
Member

@John-Qiao the debugger issue doesn't have anything to do with my change. Not sure what that would be coming from.

This was serviced back to .NET 9, the first update is in January.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🪲 bug Product bug (most likely) 💥 regression-release Regression from a public release 🚧 work in progress Work that is current in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants