c# - 如何在 wpf 中显示 avif 格式的图像?
问题描述
我之前通过发布此问题解决了 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");
}));
}
解决方案
推荐阅读
- r - 可以将前一行值按两个不同的列分组吗?
- python - django 模型 - 检查具有匹配属性的模型是否已存在于数据库中的实例方法
- java - Android:ZipFile() java.util.zip.ZipException:文件太短而不能成为 zip 文件:0
- javascript - Do implementations of the Web Audio API tend to make full use of available CPU cores?
- sql-server - How to refresh database to test from production with encrypted columns?
- dart - 如何完成一个未来?
- sql - 使用列中的现有值更新列中的空值
- reactjs - Problem trying to put a variable value into my Link to value for the URL
- python - 列表中的迭代迭代
- javascript - 使用 mocha/chai 测试几个函数回调