GemBox Support Forum

AutoFit feature doesn't work under Linux in .NET 6


This code will raise exception like this when run in a Linux container with .NET 6:

The type initializer for ‘‘Gdip’’ threw an exception.
System.Drawing.Common is not supported on non-Windows platforms. See Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Docs for more information.’, StackTrace: ’ at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, IntPtr scan0, IntPtr& bitmap)

Not sure if this is a known issue.


Yes, this is a breaking change that occurred with System.Drawing.Common package after version 6.0.0.

To resolve this you can either install an older version 5.0.0:

Install-Package System.Drawing.Common -Version 5.0.0

Or you can keep the latest version and enable support for non-Windows platforms in .NET 6 by setting the System.Drawing.EnableUnixSupport runtime configuration switch to true in the runtimeconfig.json file:

   "configProperties": {
      "System.Drawing.EnableUnixSupport": true

Last just as an FYI, note that we’re currently in process of removing the System.Drawing dependency.
This improvement should be done within a few months.


Following this solution, and with a container restart, now the error has gone. The only thing that bothers is that the AutoFit() function is not working. The generated excel file doesn’t adjust the column width as expected.

You are probably missing the font file on the container image. They are not included by default. Without it AutoFit() is not able to measure the text in cells and cannot adjust the column width. Here you can find some general information on how to install Microsoft fonts on Linux.

Thanks for providing the link. After a quick review, I decide just not to have the AutoFit feature. Not quite a big deal for my project. LOL.