Search code examples
c#.net-coresystem.drawingmultiplatform

Using System.Drawing package from multiple machines (multiple OS)


We are building a .Net Core 2.0 C# web app that uses System.Drawing package.

I am writing code on Mac, but it will be also developed on the Windows machine, and deployed into Linux server.

To use System.Drawing on OSX I had to add runtime.osx.10.10-x64.CoreCompat.System.Drawing Nuget package to my project dependencies.

But that code now doesn't work on the Window machine. We get the System.AccessViolationException when trying to use DrawString method of Graphics (part of System.Drawing) object.

So, my question is what is common way to solve this? Is it even possible to use System.Drawing classes in this multi-os setup?


Solution

  • The same System.Drawing code should work on Linux, Windows and macOS without any changes. You're correct that you need to either manually install libgdiplus (e.g. via brew install mono-libgdiplus) or reference runtime.osx.10.10-x64.CoreCompat.System.Drawing for System.Drawing to work on a Mac.

    System.Drawing will work on Windows without any external dependencies.

    If you're still using CoreCompat.System.Drawing (which I maintain), I'd recommend you migrate to System.Drawing.Common instead - this the is Microsoft implementation of System.Drawing for .NET Core.

    If the issue then still reproduces on Windows, your best bet would be copy a should piece of code which can be used to reproduce your issue, so I can have a look at it.