82

I have updated my project from .NET 4.5 to .NET Core (with ASP.NET Core). I had some very simple code in my previous version that used the bitmap object from System.Drawing to resize an image.

As I understand System.Drawing cannot be used in .NET Core because it is not cross platform, but what can be used instead?

I have googled this and cannot find anything. The only thing I can find is this post, which has no code on it what so ever.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • Found [this](http://imageresizing.net/) on the github you linked. Helpful? – SimpleVar Oct 26 '15 at 11:15
  • Possible duplicate of [Resize images with MVC 6 on Ubuntu running ASP.NET 5 on Mono](http://stackoverflow.com/questions/36643720/resize-images-with-mvc-6-on-ubuntu-running-asp-net-5-on-mono) – Bart Calixto Apr 20 '16 at 07:03
  • To my understanding the .NET (Core) team is still discussing this with the Xamarin team. Xamarin built a library called SkiaSharp which is a wrapper for the cross-platform Skia library which again is the graphic library of Google Chrome. It runs under Android, iOS, Linux, Windows, etc. which would make it a perfect match for .NET Core. But as of today, they have not finished it for .NET Core (https://blog.xamarin.com/cross-platform-2d-graphics-with-skiasharp/). My bets go, that this library will be the future story for .NET Core and drawing. – Thomas May 21 '16 at 19:37
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – TomTom May 24 '16 at 07:33
  • 1
    Add more facts to assist @Thomas [SkiaSharp can be dated back to 2012 as part of XobotOS](http://corefx.strikingly.com/). Xamarin made it available recently, so it can be used individually, or used as the basis for future 2D rendering framework (such as System.Drawing on SkiaSharp instead of GDI+/libgdiplus). .NET Core support is tracked in https://github.com/mono/SkiaSharp/issues/20 – Lex Li Jul 10 '16 at 03:38
  • I'd be interested in advanced image manipulation like erode / dilate for core – user3791372 Feb 18 '17 at 04:16
  • 1
    Updated link on this topic posted January 19, 2017 by Bertrand Le Roy https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/ – phanf Jun 12 '17 at 15:37
  • Have you checked out [SkiaSharp](https://github.com/mono/SkiaSharp) ? Awesome library that I'm using in my project now. – Lennart Hammarström Dec 21 '17 at 16:53
  • Did you ever find a good solution for this? We mainly need it to convert between file formats and to get the image size and DPI. – David Thielen Feb 23 '20 at 23:46
  • I don't know about earlier versions of core but .NET 5 has System.Drawing integrated, it would appear. I was able to manipulate images from aspnetcore project. – Josh Sutterfield Jun 28 '21 at 17:46

4 Answers4

48

Disclaimer: This is my software.

I'm working on a cross-platform 2D Graphics library that runs on .NET Core It's currently alpha but already supports a comprehensive feature set.

https://github.com/JimBobSquarePants/ImageSharp

Example usage.

using (FileStream stream = File.OpenRead("foo.jpg"))
using (FileStream output = File.OpenWrite("bar.jpg"))
{
    Image image = new Image(stream);
    image.Resize(image.Width / 2, image.Height / 2)
         .Greyscale()
         .Save(output);
}
James South
  • 10,147
  • 4
  • 59
  • 115
  • So what about drawing on a specific OS canvas? has anyone abstracted that away from the OS? –  Sep 08 '16 at 17:46
  • TK has a .Net Core library for graphics. I expect a UI lib eventually but it might wrap TK. – IAbstract May 09 '17 at 12:53
  • @IAbstract TK, you got a link to share? – James South May 10 '17 at 00:41
  • 1
    Just perfect. Zero dependencies and full range of formats. Thanks! – Alek Depler Aug 26 '21 at 10:37
  • Please note that ImageSharp changed their license and is PAID if your company makes more than 1M/year – Alex from Jitbit Mar 13 '23 at 16:17
  • How is that relevant to the question? – James South Mar 14 '23 at 04:34
  • @JamesSouth this is not relevant to the question, but this is relevant to the answer. It was posted in 2016 linking to an Apache-licensed library. Then the license has changed, but developers rarely pay attention when updating `nugets`. These things come up during legal audits later and can be very painful. – Alex from Jitbit Mar 17 '23 at 10:36
  • Except that it's not. The question does not specify that OSI licenses are required and the answer does not specify that the library has an OSI license. This is a forum for technical QA and licensing discussions are explicitly off topic. https://stackoverflow.com/help/on-topic#:~:text=Legal%20questions%2C%20including%20questions%20about%20copyright%20or%20licensing%2C%20are%20off%2Dtopic%20for%20Stack%20Overflow.%20Open%20Source%20Stack%20Exchange%20or%20Law%20Stack%20Exchange%20may%20be%20suitable%20alternatives.. – James South Mar 18 '23 at 11:50
38

You can use now official (from Microsoft) System.Drawing.Common NuGet package.


Note that's it's Windows-only starting from .NET 6 (it used to be cross-platform for .NET Core and .NET 5). It will still work in .NET 6 with special switch on other platforms.

From Microsoft docs recommended action:

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

Alternatively, you can 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
   }
}

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, this switch has been removed in .NET 7.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
  • @DougS No, it's cross platform. See discussion here https://github.com/dotnet/corefx/issues/20325 – Vadim Ovchinnikov May 07 '18 at 19:04
  • @DougS Yeah, I agree that headline is confusing. – Vadim Ovchinnikov May 08 '18 at 08:34
  • See Scott Hanselman's blogg on how to use System.Drawing.Common: https://www.hanselman.com/blog/HowDoYouUseSystemDrawingInNETCore.aspx – sevenam Jun 18 '20 at 08:55
  • This is bad solution due to dependencies and huge size. See @James South answer – Alek Depler Aug 26 '21 at 10:38
  • @AlekDepler At the moment of writing answer (in 2018) library in James South answer was really buggy. I tested it and many alternatives back then. Probably it's not the best but at least I can recommend System.Drawing.Common library for enterprise solutions. It provides consistent and reliable results. – Vadim Ovchinnikov Aug 27 '21 at 09:31
  • 1
    @VadimOvchinnikov System.Drawing.Common is unsupported on non Windows platforms as of .NET 6 https://learn.microsoft.com/en-us/dotnet/api/system.drawing?view=net-6.0#remarks – James South May 25 '22 at 13:40
  • 1
    @JamesSouth I added information about .NET 6 non-Windows platforms in .NET 6. I'm grateful for your update, we didn't manage to migrate to .NET 6 so I didn't know about this. – Vadim Ovchinnikov May 25 '22 at 14:16
16

I've found an implementation of System.Drawing for .NET Core based on Mono's sources being maintained at:

The NuGet package is at:

Which you can reference it in your .NET Core App's project.json with:

{
  "dependencies": {
    "CoreCompat.System.Drawing": "1.0.0-beta006",
    ...
  },
}
mythz
  • 141,670
  • 29
  • 246
  • 390
-1

Aspose.Drawing can manipulate images using API compatible with System.Drawing, fully managed and cross-platform with .NET Core 2.0+ support. (I'm one of the developers.)

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66