Bye by System.Drawing and GDI+

As a .NET developer, you have been for sure using in some projects at least some classes from the namespace System.Drawing. The implementation behind this namespace is design to be used on Windows OS. That means, the classes in this namespace depend on the GDI+, which is the native graphic library on Windows. Some Windows versions, like Windows Server Core or Windows Nano, don't include GDI+.

On the macOS and Linux, the GDI+ functionality is implemented by the libgdiplus.
If you use this namespace and the library can't be loaded, exceptions will be thrown at run time. .NET 5 and some previous of System.Drawing.Common do support also MacOS and Linux. However the libgdiplus must be explicitly installed.
As you see, using of System.Drawing on the cross-platform is still an issue.
So, the question is now, how .NET developers should deal with this?

There are few options:

-Windows Imaging Components, and

The answer is, you have to migrate to the preferred version. Personally, I expect that MAUI will become over time the preferred cross-platform library inside .NET for graphic related tasks.
To get a flavour of the migration, please take a look at the following code.
The code snippet shows the old version with System.Drawing that creates the Bitmap from the memory stream and saves it to the file. This code does not run on Linux OS. To make it cross-platform, I have migrated it to the MAUI.

MemoryStream imageMemoryStream = ...

   //Convert to Image
   Image image = Image.FromStream(imageMemoryStream);

   //Convert to Bitmap
   Bitmap bitmapImage = (Bitmap)image;

Following code-snippet does the same task by using a MAUL API.


MemoryStream someStream = ...

using (MemoryStream imageMemoryStream = new MemoryStream())
    byte[] imageData = someStream.ToArray();

    IImage image = GraphicsPlatform.CurrentService.LoadImageFromBytes(imageData);
    using (Stream imgStream = 
    new FileStream(imgFullPathName, FileMode.OpenOrCreate))

As you see, migration was an easy task. But, in large projects with a lot of System.Drawing code, might be a hard task could take a longer time. Independent on migration, if you design new applications, avoid using System.Drawing
Last, but not least, the MAUI is at the writing of this post available in the preview version of the following NUGET package:

Microsoft.Maui.Graphics Version="6.0.200-preview.12.852

comments powered by Disqus