Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-side graphics in ASP.NET Core

I've recently upgraded an ASP.NET MVC application from ASP.NET to ASP.NET Core.

In my controller action, I had a piece of code that relied on System.Drawing to create a profile picture

using (FileStream stream = new FileStream(HttpContext.Server.MapPath($"~/Content/UserFiles/{AuthenticatedUser.Id.ToString()}.jpg"), FileMode.OpenOrCreate))
{
    Image image = Image.FromStream(model.DisplayPicture.InputStream);
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

The image data is posted to the server as a Base64 encoded image

data:image/png;base64,....

Since there's no System.Drawing in .NET Core, are there any other libraries that can achieve this?

like image 342
Matthew Layton Avatar asked Oct 19 '22 06:10

Matthew Layton


3 Answers

As Stanislav pointed out the current solution is to use ASP.NET Core on the full .NET framework. System.Drawing relies on GDI+ calls an is therefore bound to Windows.

The vNext Version of Image Resizer by Imazen will solve this problem based on the new imageflow project. System.Drawing should not be used in server environments like ASP.NET (pointed out on https://msdn.microsoft.com/en-us/library/system.drawing(v=vs.110).aspx). Some background on this topic is provided on https://github.com/imazen/Graphics-vNext.

I suggest to use the current version 4.0.5 of ImagerResizing and upgrade in some months (first stable vNext version is announced for next year).

like image 115
Ralf Bönning Avatar answered Nov 03 '22 22:11

Ralf Bönning


You don't need System.Drawing if all you are trying to do is convert base64 to image files. I'm doing that in my cloudscribe.SimpleContent project. Users add images and content in the wysiwyg editor and when it posts back to the server I'm converting them to files and resolving the urls for the files to update the content so it links to the new file.

You can see my working code here: https://github.com/joeaudette/cloudscribe.SimpleContent/blob/master/src/cloudscribe.SimpleContent.Web/Services/FileSystemMediaProcessor.cs

What we do need System.Drawing or some other tool for is resizing and optimizing images. There is an ImageSharp project here that is working on that though I'm not sure how much functionality is currently ready

like image 22
Joe Audette Avatar answered Nov 03 '22 22:11

Joe Audette


Just came across your question. Now in 2019 the problem to use System.Drawing in ASP.NET Core and on the server side still needs some investigation.

I found the package called "System.Drawing.Common" in NuGet, which has the API like System.Drawing and can be used as a replacement.

For more information about this packege is here a list of packages, comparing performance and quality.

like image 28
jboi Avatar answered Nov 03 '22 23:11

jboi