Server-side graphics in ASP.NET Core

空扰寡人 提交于 2019-12-23 15:16:25

问题


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?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/38284346/server-side-graphics-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!