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

Open
bitbound opened this issue Nov 15, 2024 · 5 comments · Fixed by #12501
Open

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

bitbound opened this issue Nov 15, 2024 · 5 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.

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.

3 participants