WPF Can't retrieve WebP image from url?

谁说胖子不能爱 提交于 2021-02-05 09:31:13

问题


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.

Code for retrieving image:

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;

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.

Update:

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;

回答1:


WPF does not natively support the WebP image format.

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

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:

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;
}

I've tested it with

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


来源:https://stackoverflow.com/questions/65288683/how-to-display-webp-format-image-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!