最近在處理一些圖形的東西,但是因為就是很懶,所以網路上找了一下有沒有 .NET 可以方便撰寫的 library
測試了幾個最後推薦這個 Magick.NET

今天主要案例就是我要把 webp 這格式轉成 JPG ,之前文章我有寫過 把圖片轉 webp 可以參考這一篇 [C#] 圖片轉 WebP | 當麻許的超技八
試用了一下,這裡面我使用的案例圖片是來自於 wiki ,主要測試有把一張 webp 轉成 jpg ,
還有降低壓縮品質(預設 75 改成 20),還有一個就是做等比例縮小
縮小到只剩下 10%
1. Nuget 安裝 Magick.NET.Core
, Magick.NET-Q16-AnyCPU
2. 因為我撰寫測試是用 ASP.net 的關係,所以我是去讀取 wwwroot 的 sample.webp 的圖片
private readonly IWebHostEnvironment environment;
public IndexModel(IWebHostEnvironment environment)
{
this.environment = environment;
}
public IActionResult OnPostConvertToJPG()
{
var sampleWebp = new MagickImage(environment.WebRootPath +Path.DirectorySeparatorChar + "sample.webp");
sampleWebp.Format = MagickFormat.Jpeg;
sampleWebp.Write(new FileInfo(environment.WebRootPath+ Path.DirectorySeparatorChar + "sample.jpg"));
//預設是 75 降低 品質換取空間
sampleWebp.Quality = 20;
sampleWebp.Write(new FileInfo(environment.WebRootPath + Path.DirectorySeparatorChar + "sample_20_quality.jpg"));
//Resize 到 10%
sampleWebp.Quality = 100;
sampleWebp.Resize(new Percentage(10));
sampleWebp.Write(new FileInfo(environment.WebRootPath + Path.DirectorySeparatorChar + "sample_10_resizie.jpg"));
sampleWebp.Dispose();
return Page();
}
result:

簡單記錄一下,如果有發現什麼好玩的,我在上來紀錄一下。
--
The bug existed in all possible states.
Until I ran the code.
如果這篇文章有幫助到您幫我分享一下,讓我有寫下去的動力...