代码之家  ›  专栏  ›  技术社区  ›  Rana Mujahid

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

  •  15
  • Rana Mujahid  · 技术社区  · 8 年前

    我想调整图像大小,并将此图像以不同大小多次保存到文件夹中。我试过ImageResizer或CoreCompat。系统但这些库与不兼容。净核心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");
    }
    

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

    4 回复  |  直到 8 年前
        1
  •  8
  •   Frederik Carlier    8 年前

    .NET Core 2.0附带系统。绘画通用,这是系统的正式实现。的图纸。净核心。

    而不是CoreCompat。系统绘图时,是否可以尝试安装系统。绘画常见,并检查是否有效?

        2
  •  6
  •   valentasm    7 年前

    你可以得到六个劳工的nuget包裹。ImageSharp(不要忘记勾选“包括预发布”,因为现在他们只有beta版),并像这样使用他们的库。他们的 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.
    }
    
        3
  •  0
  •   greyxit    8 年前
        4
  •  0
  •   Lilith River    6 年前

    Imageflow。NET服务器是。NET Core相当于ImageResizer,但速度更快,生成的图像文件更小。看见 https://github.com/imazen/imageflow-dotnet-server

    如果您只是在上传过程中调整大小,或者想要编写自己的中间件,请使用Imageflow。NET直接。看见 https://github.com/imazen/imageflow-dotnet

    [免责声明:我是ImageResizer和Imageflow的作者]