0

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");
  }));
}
bass9030
  • 11
  • 1
  • 2
  • This is a specialized encoding, so you will need a specialized decoder. In short you will need a library :) – TheGeneral Dec 19 '20 at 01:26
  • Successfully resolved this issue. I implemented avifdec.exe in [this](https://github.com/AOMediaCodec/libavif) library as a process, re-encoded it with png, and then displayed the encoded image. – bass9030 Dec 20 '20 at 00:51

0 Answers0