【问题标题】:Get thumbnail from webp image .net core 3.1从 webp image .net core 3.1 获取缩略图
【发布时间】:2021-02-10 16:12:28
【问题描述】:

我有一个上传控制器,可以将图像保存到文件并从上传的文件中获取缩略图。 它适用于 .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();
....
}

【问题讨论】:

    标签: .net image .net-core asp.net-core-3.1 webp


    【解决方案1】:

    安装 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();
            }
        }
    

    当然你可以通过 resize 方法调整它的大小:

    ImageFactory.Resize...
    

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

    working with webp in .net core


    还有另一个neget包:

    ImageProcessor.NetCore
    ImageProcessorWebP.NetCore
    

    【讨论】:

      猜你喜欢
      • 2020-06-11
      • 2021-02-26
      • 2020-11-23
      • 2015-12-17
      • 2020-12-13
      • 2020-05-22
      • 2021-09-25
      • 1970-01-01
      相关资源
      最近更新 更多