如何在ASP.NET Core 2.0中上载后调整图像大小

Ran*_*hid 9 system.drawing resize-image asp.net-core-2.0

我想调整图像大小并将此图像以不同的大小多次保存到文件夹中.我已经尝试过ImageResizer或CoreCompat.System.Drawing,但是这些库与.Net core 2不兼容.我已经搜索了很多关于这个,但我找不到任何合适的解决方案.像在MVC4我用过:

public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null)
{
    var versions = new Dictionary<string, string>();

    var path = Server.MapPath("~/Images/");

    //Define the versions to generate
    versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg";);
    versions.Add("_medium", "maxwidth=900&maxheight=900&format=jpg");
    versions.Add("_large", "maxwidth=1200&maxheight=1200&format=jpg");

    //Generate each version
    foreach (var suffix in versions.Keys)
    {
        file.InputStream.Seek(0, SeekOrigin.Begin);

        //Let the image builder add the correct extension based on the output file type
        ImageBuilder.Current.Build(
            new ImageJob(
                file.InputStream,
                path + file.FileName + suffix,
                new Instructions(versions[suffix]),
                false,
                true));
    }
}

return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)

但是在Asp.Net核心2.0中我被卡住了.我不知道如何在.Net核心2中实现这一点.任何人都可以帮助我.

Fre*_*ier 7

.NET Core 2.0附带了System.Drawing.Common,它是System.Drawing for .NET Core的正式实现。

您可以尝试安装System.Drawing.Common而不是CoreCompat.System.Drawing来检查它是否有效吗?


val*_*asm 5

你可以得到 nuget 包 SixLabors.ImageSharp(不要忘记勾选“包括预发布”,因为现在他们只有测试版)并像这样使用他们的库。他们的GitHub

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

// Image.Load(string path) is a shortcut for our default type. 
// Other pixel formats use Image.Load<TPixel>(string path))
using (Image<Rgba32> image = Image.Load("foo.jpg"))
{
    image.Mutate(x => x
         .Resize(image.Width / 2, image.Height / 2)
         .Grayscale());
    image.Save("bar.jpg"); // Automatic encoder selected based on extension.
}
Run Code Online (Sandbox Code Playgroud)