ImageFactory正在生成大于.jpeg文件的.webp文件

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

我有一个基于ASP.NET Core的WebAPI。我正在尝试将上传的.jpeg图像转换为.webp。我尝试使用ImageProcessor库和ImageProcessor.Plugins.WebP来生成.webp压缩文件。这是我使用的代码

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

    string absoluteFilename = Path.Combine("d:/uploaded_images", Path.GetRandomFileName() + ".webp");

    using var stream = new FileStream(absoluteFilename, FileMode.Create);
    using ImageFactory imageFactory = new ImageFactory(preserveExifData: false);
    imageFactory.Load(file.OpenReadStream())
                .Format(new WebPFormat())
                .Quality(100)
                .Save(stream);

    return Ok(absoluteFilename);
}

但是以上代码使用83.9KB JPEG文件并创建了379KB WEBP文件。我尝试使用an online converter将JPEG文件转换为WEBP,结果为73KB。

如何正确地将.jpeg文件转换为.webp

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

我检查了此程序包的源代码,我认为在将源图像转换为Bitmap期间,很多压缩优势都丢失了。我尝试使用Google的工具将文件转换为webp,并将图像文件从100 KB减少到74 KB。您可以将其嵌入到您的项目中。

在Web环境中启动exe可能很棘手,但是您可以查看有关此主题的一些文章http://www.codedigest.com/articles/aspnet/334_working_with_exe_in_aspnet-calling_killingaborting_an_exe.aspx

关于cwebp的更多信息,请点击此处https://developers.google.com/speed/webp/docs/cwebp

下载链接https://developers.google.com/speed/webp/download

using System;
using System.Diagnostics;

namespace ConsoleApp15
{
    class Program
    {
        static void Main(string[] args)
        {
            var filePath = @"C:\Users\jjjjjjjjjjjj\Downloads\libwebp-1.0.3-windows-x86\bin\cwebp.exe";
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = filePath;
            startInfo.Arguments = "file.jpeg -o file.webp";

            startInfo.CreateNoWindow = true; // I've read some articles this is required for launching exe in Web environment
            startInfo.UseShellExecute = false;
            try
            {
                using (Process exeProcess = Process.Start(startInfo))
                {
                    exeProcess.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.