如何使用.NET Core将JPEG转换为WebP格式?

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

我有一个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格式存储图像?

我有一个ASP.NET Core 3.1 WebAPI,它负责为我拥有的其他应用提供图像。我正在尝试提高应用程序的性能。我通过运行PageSpeed获得的反馈之一...

c# asp.net-core asp.net-core-webapi webp asp.net-core-3.1
1个回答
0
投票

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

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