我有一个基于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
?
我检查了此程序包的源代码,我认为在将源图像转换为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();
}
}
}
}