C# でWebPを読み書きする


画像処理などで使う画像をWebPに変換して容量減らせればいいなーと思ってやり方を調べたので
備忘録的に書いておきます。

Nugetで「ImageProcessor」パッケージをインストールする

Visual Studio 2022 .Net Framework 4.8で動作を確認しています。
他の環境は未チェックですのであしからず。

WebPから、jpegやpng等に変換して保存する

public static void Save(string inPutPath, string outPutPath, ImageFormat format)
{
    var wf = new WebPFormat();

    using (var image = (Bitmap)wf.Load(new FileStream(inPutPath, FileMode.Open, FileAccess.Read)))
    {
        image.Save(outPutPath, format);
    }
}

jpegやpng等から、WebPに変換して保存する

Taskで並列に変換&保存できてるっぽいけど、すぐにCPU使用率が100%になる…

public static void SaveWebP(string inPutPath, string outPutPath)
{
    var wf = new WebPFormat();

    using (var image = new Bitmap(inPutPath))
    {
        wf.Save(outPutPath, image, 0);
    }
}