Manipulating images with .NET Core
Asked Answered
P

4

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.

Pleuron answered 26/10, 2015 at 11:7 Comment(10)
Found this on the github you linked. Helpful?Peacemaker
Possible duplicate of Resize images with MVC 6 on Ubuntu running ASP.NET 5 on MonoDickey
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 (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.Gemmulation
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.Calandra
Add more facts to assist @Gemmulation SkiaSharp can be dated back to 2012 as part of XobotOS. 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 github.com/mono/SkiaSharp/issues/20Ashantiashbaugh
I'd be interested in advanced image manipulation like erode / dilate for coreCureall
Updated link on this topic posted January 19, 2017 by Bertrand Le Roy blogs.msdn.microsoft.com/dotnet/2017/01/19/…Locksmith
Have you checked out SkiaSharp ? Awesome library that I'm using in my project now.Howe
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.Triangulate
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.Maurey
E
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);
}
Evadnee answered 23/5, 2016 at 23:42 Comment(8)
So what about drawing on a specific OS canvas? has anyone abstracted that away from the OS?Drill
TK has a .Net Core library for graphics. I expect a UI lib eventually but it might wrap TK.Originative
@Originative TK, you got a link to share?Evadnee
Just perfect. Zero dependencies and full range of formats. Thanks!Keslie
Please note that ImageSharp changed their license and is PAID if your company makes more than 1M/yearNoland
How is that relevant to the question?Evadnee
@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.Noland
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. stackoverflow.com/help/…..Evadnee
U
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.

Unburden answered 27/1, 2018 at 10:35 Comment(7)
@DougS No, it's cross platform. See discussion here github.com/dotnet/corefx/issues/20325Unburden
@DougS Yeah, I agree that headline is confusing.Unburden
See Scott Hanselman's blogg on how to use System.Drawing.Common: hanselman.com/blog/HowDoYouUseSystemDrawingInNETCore.aspxPargeting
This is bad solution due to dependencies and huge size. See @James South answerKeslie
@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.Unburden
@VadimOvchinnikov System.Drawing.Common is unsupported on non Windows platforms as of .NET 6 learn.microsoft.com/en-us/dotnet/api/…Evadnee
@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.Unburden
F
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",
    ...
  },
}
Furlani answered 22/9, 2016 at 0:32 Comment(2)
This is prerelease software, so make sure you have the "show prerelease" box ticked.Cureall
This package has been marked deprecated and has not received any updates since June 27, 2016.Bloodletting
M
-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.)

Malachi answered 5/11, 2020 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.