从 webp image .net core 3.1 获取缩略图

Get thumbnail from webp image .net core 3.1

我有一个上传控制器,可以将图像保存到文件并从上传的文件中获取缩略图。 它与 .jpeg 或 .png 一起使用,但我如何从 webp 扩展名获取缩略图并将其另存为 .webp。 我收到错误参数无效

uploadImage(IFormFile file){
....
Stream filestream = new FileStream(filePath, FileMode.Create);
await file.CopyToAsync(filestream);
var myBitmap = Image.FromStream(filestream);  <----- error: parameter is not valid 

using (var myThumbnail = myBitmap.GetThumbnailImage(150, 150, () => false, IntPtr.Zero))
{
   //e.Graphics.DrawImage(myThumbnail, 150, 75);   // scale main image if thum is big 
   size like 300 * 300
   myThumbnail.Save(thumbFullPathName);
}
filestream.Close();
....
}

安装 ImageProcessor nuget 包:

Install-Package System.Drawing.Common
Install-Package ImageProcessor
Install-Package ImageProcessor.Plugins.WebP

及保存方法:

private void SaveAsThumbnail(IFormFile file, string path, string unicFileName)
    {
        string thumbFolder = Path.Combine(Directory.GetCurrentDirectory(), rootDir, "thumb", path);
        Directory.CreateDirectory(thumbFolder);
        string thumbFullPathName = Path.Combine(thumbFolder, unicFileName);
        using (var webPFileStream = new FileStream(thumbFullPathName, FileMode.Create))
        {
            using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
            {
                imageFactory.Load(file.OpenReadStream());
                var thumb = imageFactory.Image.GetThumbnailImage(150, 150, () => false, IntPtr.Zero);
                imageFactory.Format(new WebPFormat())
                            .Quality(90)
                            .Save(webPFileStream);
            }
            webPFileStream.Close();
        }
    }

当然,您可以通过调整大小方法调整它的大小:

ImageFactory.Resize...

我从 elmah.io 网站找到了解决方案:

working with webp in .net core


还有另一个neget包:

ImageProcessor.NetCore
ImageProcessorWebP.NetCore