在 ASP.NET Core 中调整图像大小的正确方法是什么?

问题描述 投票:0回答:2

这是我第一次在 ASP.NET Core 中调整图像大小,所以经过一些研究,我发现这种方法是最简单、最有效的,我实现了如下所示。

但是,我不确定这种方法是否是最有效的方法,因为它有两个问题如下

  1. 图像质量下降很多
  2. 我在 Visual Studio 中收到此“警告”,只要我是唯一开发它的人就可以,但如果其他人这样做,情况就不一定如此 -

此调用站点可在所有平台上访问。 “位图”仅在 Windows 上受支持。

因此,我想知道还有哪些其他更合适的方法存在,并且我可以实施以至少解决这两个问题中的第一个问题。

public async Task<IActionResult> Add(AddCardFormModel card, List<IFormFile> ImageFile)
{
    // ...

    foreach (var image in ImageFile)
    {
        if (image.Length > 0 || image.Length <= (2 * 1024 * 1024))
        {
            var imagesToBeResized = Image.FromStream(image.OpenReadStream());
            var resized = new Bitmap(imagesToBeResized, new Size(250, 350));

            using (var stream = new MemoryStream())
            {
                resized.Save(stream, ImageFormat.Jpeg);

                var cardData = new Card
                        {
                            Title = card.Title,
                            Description = card.Description,
                            ImageUrl = card.ImageUrl,
                            CategoryId = card.CategoryId,
                            ConditionId = card.ConditionId,
                            Price = card.Price,
                            DealerId = dealerId,
                            Image = stream.ToArray()
                        };

                this.data.Cards.Add(cardData);
                this.data.SaveChanges();
            }
        }
    }

    // ...
}
c# asp.net-core image-resizing
2个回答
5
投票

System.Drawing.Common
软件包仅在 Windows 上受支持。

如果您希望应用程序跨平台工作,那么 Microsoft 建议迁移到以下库之一:

https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action

您应该首先决定在项目中使用哪个图像处理库,然后寻找使用该特定库调整图像大小的最佳算法。

SkiaSharp 示例:

using SkiaSharp;

public static byte[] ScaleImage(byte[] imageBytes, int maxWidth, int maxHeight)
{
    SKBitmap image = SKBitmap.Decode(imageBytes);

    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    var info = new SKImageInfo(newWidth, newHeight);
    image = image.Resize(info, SKFilterQuality.High);

    using var ms = new MemoryStream();
    image.Encode(ms, SKEncodedImageFormat.Png, 100);
    return ms.ToArray();
}

ImageSharp 示例:

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing;

public static byte[] ScaleImage(byte[] imageBytes, int maxWidth, int maxHeight)
{
    Image image = Image.Load(imageBytes);   

    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);

    image.Mutate(x => x.Resize(newWidth, newHeight));

    using var ms = new MemoryStream();
    image.Save(ms, new PngEncoder());
    return ms.ToArray();
}

0
投票

检查此以调整图像大小。

但我建议不要将图像存储在数据库中,因为数据库大小和日志大小会增加。

你有2个选择:

选项 1:将调整大小的图像存储在目录中并将名称保存在数据库中。

选项2:将原始大小存储在目录中并实时调整其大小。

© www.soinside.com 2019 - 2024. All rights reserved.