我想调整图像的大小,并将此图像以不同的大小多次保存到一个文件夹中。我尝试过ImageResizer或CoreCompat.System.Drawing,但这些库与.Net Core2不兼容。我搜索了很多关于这方面的信息,但我找不到任何合适的解决方案。就像我在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 Core2中实现这一点。任何人都可以帮助我。
发布于 2018-12-31 15:31:53
你可以得到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.
}
发布于 2018-06-01 07:17:54
.NET Core2.0附带了System.Drawing.Common,它是System.Drawing在.NET核心上的官方实现。
你能不能试着安装System.Drawing.Common并检查它是否能工作,而不是CoreCompat.System.Drawing?
发布于 2018-04-06 21:06:27
https://stackoverflow.com/questions/49693465
复制相似问题