5

We've been migrating a .NET 4.x application to .NET 8 and encountered a massive problem with System.Drawing.Bitmap. Our image processing was heavily dependent on graphic libraries, such as AForge. The AForge library uses System.Drawing.Bitmap and lockbits and manipulates the unmanaged memory of the Bitmap object.

My question: is there a wrapper for System.Drawing.Bitmap or is any effort being made to replicate the base functionality? Or, do any of Microsoft's suggested alternative libraries offer unmanaged (unsafe) access to the image memory that can be used with third-party libraries, such as AForge?

I understand Microsoft's movement on "a million bugs, etc., etc." with the mono implementation. But, thousands of libraries depend on basic System.Drawing.Bitmap functionality, which is only a tiny component of the whole System.Drawing namespace. It seems that the most popular components can be replicated to continue using the vast collection of libraries that people and companies have put effort into building.

Reference: https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only

4
  • Apologies, I assumed the mention of mono implied our cross-platform requirements. I updated the subject of the question to explicitly mention "Cross-platform" Commented Jan 21, 2024 at 20:27
  • My question is whether a drop-in replacement exists, even using the suggested replacements. The replacements do not appear to offer unmanaged/unsafe bitmap memory access compatible with the existing System.Drawing.Bitmap class. If nothing else exists, I guess an option for us is to build a System.Drawing.Bitmap stub that replicates the necessary functions for our use. Commented Jan 21, 2024 at 20:32
  • I don't have an answer yet, but this looks promising, as someone else has done what I suggested in my previous comment (a wrapper). It's just painful to see MS delete unix system.drawing when it clearly worked for businesses: reddit.com/r/dotnet/comments/16z87mm/… Commented Jan 21, 2024 at 20:55
  • After some more searches, it seems this is affecting a lot of projects. So, I'll add some of my findings here in comments to document the progress. There doesn't seem to be a suggested course of action amongst the complaints. So, hopefully, this thread will act as a good place for others to start. If we're to create a wrapper for a suggested alternative, here are performance comparisons for our consideration: devblogs.microsoft.com/dotnet/net-core-image-processing Commented Jan 21, 2024 at 21:06

1 Answer 1

7

Just discovered this issue tonight after upgrading System.Drawing.Common to 8x, but OP was the only question I found on the topic.

I went with SkiaSharp (the first alternative mentioned in the notification) for a drop-in replacement. It has no license restrictions, and appears to have a similar decomposition to the existing System.Drawing code that triggered the compiler warning.

[Generating a bitmap via System.Drawing: (This will apparently throw an error if not running windows post .Net 6)

  using (var ms = new MemoryStream())
  using (Image img = new Bitmap(1, 1)) {
      img.Save(ms, ImageFormat.Jpeg);
      var bytes = ms.ToArray();
      return this.File(bytes, "image/jpeg");
  }

And via SkiaSharp:

 using (var bmap = new SKBitmap(1, 1, false))
 using (var img = SKImage.FromBitmap(bmap))
 using (var bytes = img.Encode(SKEncodedImageFormat.Jpeg, 0)) {
    return this.File(bytes.ToArray(), "image/jpeg");
 }

I Hope this helps somebody.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.