Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ImageFactory正在生成大于.jpeg文件的.webp文件_C#_Asp.net Core_Webp_Asp.net Core 3.1_Imageprocessor - Fatal编程技术网

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

C# ImageFactory正在生成大于.jpeg文件的.webp文件,c#,asp.net-core,webp,asp.net-core-3.1,imageprocessor,C#,Asp.net Core,Webp,Asp.net Core 3.1,Imageprocessor,我有一个基于ASP.NET核心的WebAPI。我正在尝试将上载的.jpeg图像转换为.webp。我尝试使用库和生成.webp压缩文件。这是我使用的代码 public async Task<IActionResult> Store(IFormFile file) { if(!ModelState.IsValid) { return Problem("Invalid model!"); } string absoluteFilename

我有一个基于ASP.NET核心的WebAPI。我正在尝试将上载的
.jpeg
图像转换为
.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);
}
公共异步任务存储(IFormFile)
{
如果(!ModelState.IsValid)
{
返回问题(“模型无效!”);
}
字符串absoluteFilename=Path.Combine(“d:/upload_images”,Path.GetRandomFileName()+”.webp”);
使用var stream=newfilestream(绝对文件名,FileMode.Create);
使用ImageFactory ImageFactory=新建ImageFactory(preserveExifData:false);
Load(文件.OpenReadStream())
.Format(新的WebPFormat())
.品质(100)
.保存(流);
返回Ok(绝对文件名);
}
但是上面的代码采用了83.9KB的JPEG文件,并创建了379KB的WEBP文件。我尝试使用将我的JPEG文件转换为WEBP,结果是73KB


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

我检查了此软件包的源代码,我认为在将源图像转换为
位图的过程中会丢失很多压缩好处。我尝试使用Google的工具将文件转换为webp,它将图像文件的大小从100KB减少到了74KB。
您可以将其嵌入到项目中

在Web环境中启动exe可能很棘手,但您可以查看有关此主题的一些文章

有关cwebp的更多信息,请参见此处

下载链接


你应该在这里提供图像。
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();
            }
        }
    }
}