How to display webp format image in WPF?

305 Views Asked by At

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

There are 0 best solutions below