I am using .NET Core with C# on Linux, and library *System.Drawing* is missing
Asked Answered
K

6

17

Error message

Program.cs(1,14): error CS0234: The type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?

Is it possible to fix that? Or I am not meant to be using this library, if I am developing on Linux with C#?

Kofu answered 8/6, 2017 at 6:40 Comment(0)
O
19

2021:

Quote from System.Drawing.Common only supported on Windows, Recommended action:

To use these APIs for cross-platform apps, migrate to one of the following libraries:

  • ImageSharp
  • SkiaSharp
  • Microsoft.Maui.Graphics

You are able to use System.Drawing for the short term but be aware there is no support.

Alternatively, you can enable support for non-Windows platforms by setting the System.Drawing.EnableUnixSupport runtime configuration switch to true in the runtimeconfig.json file:

This configuration switch was added to give cross-platform apps that depend heavily on this package time to migrate to more modern libraries. However, non-Windows bugs will not be fixed. In addition, we may completely remove support for non-Windows platforms in a future release, even if you enable it using the runtime configuration switch.

To be able to use System.Drawing on Linux machines, you might also need to install libgdiplus as mentioned by @saman-azadi and @alex-from-jitbit.


2017:

The System.Drawing namespace is at the moment not part of corefx as it relies on the GDI+ features from Windows.

But there are plans to support it in the future.


But there are multiple alternatives:

CoreCompat.System.Drawing

CoreCompat.System.Drawing is a .NET Core port of the Mono implementation of System.Drawing. Like System.Drawing in .NET Framework and in Mono, CoreCompat.System.Drawing also relies on GDI+ on Windows. Caution is therefore advised, for the same reasons.

ImageSharp

ImageSharp is a brand new, pure managed code, and cross-platform image processing library. Its performance is not as good as that of libraries relying on native OS-specific dependencies, but it remains very reasonable. Its only dependency is .NET itself, which makes it extremely portable: there is no additional package to install, just reference ImageSharp itself, and you’re done.

Magick.NET

Magick.NET is the .NET wrapper for the popular ImageMagick library. ImageMagick is an open-source, cross-platform library that focuses on image quality, and on offering a very wide choice of supported image formats. It also has the same support for EXIF as ImageSharp.

(The .NET Core build of Magick.NET currently only supports Windows.)

SkiaSharp

SkiaSharp is the .NET wrapper for Google’s Skia cross-platform 2D graphics library, that is maintained by the Xamarin team. SkiaSharp is now compatible with .NET Core, and is extremely fast.

FreeImage-dotnet-core

This library is to the native FreeImage library what Magick.NET is to ImageMagick: a .NET Core wrapper. It offers a nice choice of image formats, good performance, and good visual quality.


Here are some good examples and performance analyses of the libraries mentioned above.

Outgrowth answered 8/6, 2017 at 6:44 Comment(1)
If you're interested in CoreCompat.System.Drawing, you might want to check out CoreCompat.System.Drawing.v2.Currency
V
6

Run the following command on the command line

sudo apt-get install libgdiplus

or

sudo yum install -y libgdiplus-2.10-9.el7.x86_64
Vashtivashtia answered 18/9, 2021 at 12:9 Comment(0)
S
3

Microsoft has released System.Drawing.Common since which works on Linux too.

UPDATE: They decided to make it "Windows only" in .NET 6. The package will show compile warnings, since it relies on libgdiplus which might not be present on some Linux distribution. See System.Drawing.Common only supported on Windows.

After trying multiple alternatives we have switched to PhotoSauce (I'm not affiliated, just a recommendation) after trying multiple libraries like ImageSharp (slow, memleaks) SkiaSharp (not managed code) and ImageMagick (not managed code)

Shulem answered 14/4, 2021 at 7:36 Comment(1)
Hello did you by any chance did watermarking? putting a image logo as a watermark on top of the image with ImageSharp?Bespoke
G
0

For those trying to use System.Drawing on Linux:

libgdiplus causes a serious memory leak on Linux (I tried it on Ubuntu 20.04 (Focal Fossa)) and should not be used (found here).

ImageSharp works hard with native code (passing memory pointers) and OpenCvSharp4 became my final choice.

Gangway answered 11/1, 2022 at 2:50 Comment(0)
S
0

I am running Ubuntu Linux and I had the same exact issue. I got it working though. I found out that -pkg:dotnet is required when compiling with the mono compiler even though it isn't when using the Microsoft compiler.

C_Sharp_mono:
    mcs main.cs -pkg:dotnet && mono main.exe
C_Sharp_csc:
    csc main.cs && mono main.exe
Sidnee answered 7/7, 2023 at 7:18 Comment(0)
L
-3

You can use the Aspose.Drawing library – a drop-in replacement for System.Drawing that is fully managed and cross-platform. (I'm one of the developers.)

Langley answered 3/11, 2020 at 16:35 Comment(1)
yeah, but at $4k price. Nope thanks.Limpet

© 2022 - 2024 — McMap. All rights reserved.