WPF 无法从 url 检索 WebP 图像? [英] WPF Can't retrieve WebP image from url?

查看:36
本文介绍了WPF 无法从 url 检索 WebP 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从网址检索图像.以前,在设置 HttpClient 标头之前,我根本无法连接到该站点.我可以从其他来源检索图像,但不能从这个特定来源检索图像.

I'm unable to retrieve an image from a url. Previously I was unable to connect to the site at all until I set HttpClient headers. I'm able to retrieve images from other sources but not this particular one.

获取图片的代码:

var img = new BitmapImage();
        img.BeginInit();
        img.UriSource = new Uri("https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp", UriKind.RelativeOrAbsolute);
        img.EndInit();
        Console.Out.WriteLine();
        ImageShoe.Source = img;

如果我尝试使用不同的 url 检索不同的图像,例如 https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png 效果很好.

If I try to retrieve a different image using a different url, for example https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png it works fine.

更新:

似乎使用字节数组是可行的方法,但我仍然不确定这里有什么问题.

Seems that using a byte array is the way to go but I'm still not sure what is wrong here.

        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        var url = "https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp";//baseUrl + productUrl;
        var result = await client.GetByteArrayAsync(new Uri(
        MemoryStream buf = new MemoryStream(result);
        var image = new BitmapImage();
        image.StreamSource = buf;
        this.ImageShoe.Source = image;

推荐答案

WPF 本身不支持 WebP图片格式.

WPF does not natively support the WebP image format.

您可以通过在请求 URL 中使用 fmt=png 而不是 fmt=webp 来简单地请求支持的格式,例如 PNG:

You could simply request a supported format like PNG, by using fmt=png instead of fmt=webp in the request URL:

ImageShoe.Source = new BitmapImage(
    new Uri("https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=png"));

<小时>

如果您确实需要 WebP 支持,以下方法会下载一个 WebP 图像,并首先在 用于 .NET 的 libwebp 包装器 库.然后第二次转换从 System.Drawing.Bitmap 转换为 BitmapImage:


If you really need WebP support, the following methods downloads a WebP image and first converts it to a System.Drawing.Bitmap with help of the libwebp wrapper for .NET library. A second conversion then converts from System.Drawing.Bitmap to BitmapImage:

包装库可通过 NuGet 获得,但您还必须下载所需平台(即 x86 或 x64)的包装libwebp 库,如包装库主页所述.

The wrapper library is available via NuGet, but you also have to download the wrapped libwebp library for the desired platform, i.e. x86 or x64, as explained on the wrapper library's home page.

private async Task<BitmapImage> LoadWebP(string url)
{
    var httpClient = new HttpClient();
    var buffer = await httpClient.GetByteArrayAsync(url);
    var decoder = new Imazen.WebP.SimpleDecoder();
    var bitmap = decoder.DecodeFromBytes(buffer, buffer.Length);
    var bitmapImage = new BitmapImage();

    using (var stream = new MemoryStream())
    {
        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        stream.Position = 0;

        bitmapImage.BeginInit();
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.StreamSource = stream;
        bitmapImage.EndInit();
    }

    return bitmapImage;
}

我已经测试过了

ImageShoe.Source = await LoadWebP(
    "https://i1.adis.ws/i/jpl/jd_083285_a?qlt=80&w=600&h=425&v=1&fmt=webp");

这篇关于WPF 无法从 url 检索 WebP 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆