在 ASP.NET Core 2.0 中上传后如何调整图像大小
How to resize image after being uploaded in 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");
}
但在 Asp.Net 核心 2.0 中我被卡住了。我不知道如何在 .Net 核心 2 中实现它。任何人都可以帮助我。
.NET Core 中的图像处理:https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/
.NET Core 2.0 附带 System.Drawing.Common,这是 .NET Core 的 System.Drawing 的官方实现。
而不是 CoreCompat。System.Drawing,您可以尝试安装 System.Drawing.Common 并检查它是否有效?
你可以获得 nuget 包 SixLabors.ImageSharp(不要忘记勾选 "Include prereleases" 因为现在他们只有测试版)并像这样使用他们的库。他们的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.
}
Imageflow.NET 服务器是相当于 ImageResizer 的 .NET Core,但速度要快得多并且生成的图像文件要小得多。参见 https://github.com/imazen/imageflow-dotnet-server
如果您只是在上传时调整大小,或者想编写自己的中间件,请直接使用Imageflow.NET。参见 https://github.com/imazen/imageflow-dotnet
[免责声明:我是 ImageResizer 和 Imageflow 的作者]
我想调整一张图片的大小,并多次将这张图片以不同的尺寸保存到一个文件夹中。我尝试过 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");
}
但在 Asp.Net 核心 2.0 中我被卡住了。我不知道如何在 .Net 核心 2 中实现它。任何人都可以帮助我。
.NET Core 中的图像处理:https://blogs.msdn.microsoft.com/dotnet/2017/01/19/net-core-image-processing/
.NET Core 2.0 附带 System.Drawing.Common,这是 .NET Core 的 System.Drawing 的官方实现。
而不是 CoreCompat。System.Drawing,您可以尝试安装 System.Drawing.Common 并检查它是否有效?
你可以获得 nuget 包 SixLabors.ImageSharp(不要忘记勾选 "Include prereleases" 因为现在他们只有测试版)并像这样使用他们的库。他们的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.
}
Imageflow.NET 服务器是相当于 ImageResizer 的 .NET Core,但速度要快得多并且生成的图像文件要小得多。参见 https://github.com/imazen/imageflow-dotnet-server
如果您只是在上传时调整大小,或者想编写自己的中间件,请直接使用Imageflow.NET。参见 https://github.com/imazen/imageflow-dotnet
[免责声明:我是 ImageResizer 和 Imageflow 的作者]