【问题标题】:How to convert JPEG to WebP format using .NET Core?如何使用 .NET Core 将 JPEG 转换为 WebP 格式?
【发布时间】:2020-05-30 15:42:46
【问题描述】:

我有一个 ASP.NET Core 3.1 WebAPI,它负责为我拥有的其他应用程序提供图像。我正在努力提高我的应用程序的性能。我从运行PageSpeed Insights 测试中得到的反馈之一是Serve images in next-gen formats

我的 WebAPI 服务器的所有图像都是 JPEG 格式。我对图像压缩或图像格式了解不多,但我想评估将 JPEG 图像转换为 WebP 的结果,因为study 表明 WebP 会带来更好的结果。

我使用以下 ImageProcessor 类将上传的图像保存到我的 WebAPI。

public class ImageProcessor
{
    public Image GetResizedImage(Stream stream, int newWidth, out ImageFormat imageFormat)
    {
        Image sourceImage = Image.FromStream(stream);
        SizeF thumbSize = GetNewSize(sourceImage, newWidth);

        imageFormat = sourceImage.RawFormat;
        return ResizeImage(sourceImage, (int)thumbSize.Width, (int)thumbSize.Height);
    }

    protected SizeF GetNewSize(Image img, int newMaxWidth)
    {
        var size = new SizeF
        {
            Width = newMaxWidth,
            Height = (newMaxWidth * img.Height) / img.Width
        };

        return size;
    }

    protected Bitmap ResizeImage(Image image, int width, int height)
    {
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        var xRes = Math.Max(Math.Min(image.HorizontalResolution, 96), 72);
        var yRes = Math.Max(Math.Min(image.VerticalResolution, 96), 72);

        destImage.SetResolution(xRes, yRes);

        using (Graphics graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            using (ImageAttributes wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return destImage;
    }
}

这是我如何调用 ImageProcessor 类从控制器将图像存储在磁盘上

public async Task<IActionResult> Store(IFormFile file)
{
    if(!ModelState.IsValid) 
    {
        return Problem("Invalid model!");
    }

    // Process Image
    Image image = GetResizedImage(file.OpenReadStream(), 1024, out ImageFormat imageFormat);
    MemoryStream memoryStream = new MemoryStream();
    image.Save(memoryStream, imageFormat);
    memoryStream.Seek(0, SeekOrigin.Begin);

    // Store it on disk
    string fullPath = "full path to the new image";
    Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

    using FileStream cacheWriter = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, 4096, true);
    await memoryStream.CopyToAsync(cacheWriter);

    return Ok();
}

问题

如何使用 .Net Core 以 WebP 格式存储图像?

【问题讨论】:

标签: c# asp.net-core asp.net-core-webapi webp asp.net-core-3.1


【解决方案1】:

如果我是你,我会考虑安装 Thumbor 实例或使用 Cloudinary 生成 webp 文件。

然后就可以用JS有条件地加载正确的文件了。

【讨论】:

    猜你喜欢
    • 2015-03-18
    • 2020-08-03
    • 2020-04-24
    • 2023-02-06
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2018-12-01
    相关资源
    最近更新 更多