How to show avif format image in wpf?

119 Views Asked by At

I solved the webp problem by posting this question before. However, the avif extension is still unresolved.

I would like to display the image of this avif extension using BitmapImage in wpf image control.
However, BitmapImage does not support avif extensions and does not display.

Then how can you display an image of avif?
Below is the code I've worked on so far.

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.
  Console.WriteLine(Mydata.Length);
  wc.Dispose();
  Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
    thumbnail.Source = LoadImage(Mydata, "avif");
  }));
}
0

There are 0 best solutions below