0

我之前通过发布问题解决了 webp 问题。但是,avif 扩展仍未解决。

我想在 wpf 图像控件中使用 BitmapImage 显示这个avif 扩展的图像。
但是,BitmapImage 不支持 avif 扩展,不显示。

那怎么才能显示一张avif的图片呢?
下面是我到目前为止工作的代码。

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");
  }));
}
4

0 回答 0