Install ImageProcessor nuget package:
Install-Package System.Drawing.Common
Install-Package ImageProcessor
Install-Package ImageProcessor.Plugins.WebP
and save method:
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();
}
}
and ofcurse you can resize it by resize method:
ImageFactory.Resize...
I found solution from elmah.io web site:
working with webp in .net core
also there is another neget package:
ImageProcessor.NetCore
ImageProcessorWebP.NetCore
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…