0

I want to use a BitmapImage to display this(http://www.imageview.kro.kr/images/test.avif) image in the Image Control. (The above address is a temporary address created for this question, and the actual address contains the form avif, webp, png, and jpg.)
But, BitmapImage does not support the avif extension and does not display in the Image Control.
So how do I display the avif and webp extensions in the Image Control?
Below is the code that I've worked on so far.

Add:
I have resolved the webp support issue. Resolved using https://github.com/imazen/libwebp-net module and libwebp.dll But, support for the avif extension has not been resolved yet.
Below is the code after troubleshooting webp extension support.

private static BitmapImage LoadImage(byte[] imageData, string ext) {
  if (imageData == null || imageData.Length == 0) return null;
  var image = new BitmapImage();
  using(var mem = new MemoryStream(imageData)) {
    mem.Position = 0;
    image.BeginInit();
    image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.UriSource = null;
    if (ext == "webp") {
      SimpleDecoder dec = new SimpleDecoder();
      dec.DecodeFromBytes(imageData, imageData.Length).Save(mem, System.Drawing.Imaging.ImageFormat.Png);
      image.StreamSource = mem;
    } else if (ext == "avif") {
      //TODO: display avif format image
    }
    else {
      image.StreamSource = mem;
    }
    image.EndInit();
  }
  image.Freeze();
  return image;
}

using(WebClient wc = new WebClient()) {
  Byte[] Mydata = wc.DownloadData("http://www.imageview.kro.kr/images/test.avif");
  //The address in this code is a temporary created address,
  //but the actual address requires a header request. So I use WebClient.
  wc.Dispose();
  Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
    thumbnail.Source = LoadImage(Mydata, "avif");
  }));
}
bass9030
  • 11
  • 1
  • 2
  • Loading and saving a webp is described [here](https://stackoverflow.com/q/13220436/9363973) and the Alliance For Open Media GitHub has [this](https://github.com/AOMediaCodec/libavif) repo used for encoding and decoding avif files in C, I couldn't find anything for C# – MindSwipe Dec 14 '20 at 12:10
  • WPF uses Windows Imaging Component for codecs. There is a partial webp WIC component https://github.com/webmproject/webp-wic-codec. Not sure about avif – Mitch Dec 14 '20 at 12:16
  • In case a native codec is not available or not sufficient, you would obviously have to convert the image to a supported format first. – Clemens Dec 14 '20 at 12:36
  • ahum, put your `WebClient` in a `using` statement. – JHBonarius Dec 14 '20 at 16:36
  • I have resolved the webp support issue! Resolved using https://github.com/imazen/libwebp-net module and libwebp.dll – bass9030 Dec 15 '20 at 11:16

0 Answers0