1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】System.Drawing.Imaging.ImageFormat に WebP 形式を見つけたけど動かなくてもやった話

Posted at

はじめに

最近は WebP 画像形式 (Wikipedia) をよく目にするようになりました。ファイルサイズが比較的小さく、透過度も持てるため使いやすいです。
しかしながら比較的新しい画像形式なので、C# 標準ライブラリ System.Drawing.Bitmap クラスでは扱えなかったりします。

WPF の BitmapImage を使ったり、サードパーティのライブラリを使う方法があります。

ImageProcessor.Plugins.WebP の紹介記事
https://qiita.com/visyeii/items/9320b0d08e6b132c6541

先日 System.Drawing.Imaging.ImageFormat.Webp プロパティ を見つけました。もしかしたら Bitmap でいけるんじゃね? と思ったのですが、いけなかったので備忘録を書こうと思います。

エラーの詳細

System.Drawing.Imaging.ImageFormat.Webp プロパティ

この形式は、Windows 10 1809 以降でサポートされています。
Windows Desktop バージョン: 7, 8, 9

using var bitmap = new System.Drawing.Bitmap(10, 10);
using var stream = new MemoryStream();

// CA1416 警告: この呼び出しサイトは 'Windows' 7.0 以降 で到達可能です。
// 'ImageFormat.Webp' は 'windows' 10.0.17763.0 以降 でのみサポートされています。
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Webp);
System.ArgumentNullException :
    Value cannot be null. (Parameter 'encoder')
スタック トレース:
    at System.Drawing.CoreImageExtensions.Save(
        IImage image,
        Stream stream,
        Guid encoder,
        Guid format,
        EncoderParameters* encoderParameters
    )
    at System.Drawing.Image.Save(
        Stream stream,
        ImageFormat format
    )
  • 実行環境は Windows11 なので要件を満たしていると思うのですが、動きません
  • エンコーダーがないよ エラーが出ます
  • おま環なのかもしれません。よくわかりません

おわりに

Bitmap 関係は .NET のアセンブリではなく WindowsDesktop のアセンブリに含まれています。
C# チーム的には画像はサードパーティのライブラリを使ってね、という方針なんでしょう。
コーデックとかは、ものによってはライセンス的にあれで保守のコストが高い等の事情があるかもしれませんし。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?