85

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.

10

4 Answers 4

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);
}
8
  • So what about drawing on a specific OS canvas? has anyone abstracted that away from the OS?
    – user117499
    Commented Sep 8, 2016 at 17:46
  • TK has a .Net Core library for graphics. I expect a UI lib eventually but it might wrap TK.
    – IAbstract
    Commented May 9, 2017 at 12:53
  • 1
    Just perfect. Zero dependencies and full range of formats. Thanks! Commented Aug 26, 2021 at 10:37
  • 2
    Please note that ImageSharp changed their license and is PAID if your company makes more than 1M/year Commented Mar 13, 2023 at 16:17
  • 1
    @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. Commented Mar 17, 2023 at 10:36
40

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.

7
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",
    ...
  },
}
2
  • 2
    This is prerelease software, so make sure you have the "show prerelease" box ticked. Commented Feb 17, 2017 at 12:17
  • 1
    This package has been marked deprecated and has not received any updates since June 27, 2016.
    – BurnsBA
    Commented Sep 4, 2020 at 3:19
-5

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.)

Not the answer you're looking for? Browse other questions tagged or ask your own question.