Open neelabo opened 5 years ago
Indeed, same behavior on .NET Framework, the .webp version is broken while the .png version displays correctly.
This code:
<UniformGrid Rows="1">
<Image Source="https://www.gstatic.com/webp/gallery3/1_webp_ll.webp"></Image>
<Image Source="https://www.gstatic.com/webp/gallery3/1_webp_ll.png"></Image>
</UniformGrid>
Will produce:
(Windows version 1903 too)
https://github.com/dotnet/wpf/blob/ac9d1b7a6b0ee7c44fd2875a1174b820b3940619/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs
There is no webp decoder. So wpf use the UnknownBitmapDecoder
and it can not handle the alpha in webp format.
There is no webp decoder. So wpf use the UnknownBitmapDecoder
Thats a contradiction, I think you mean that there's no explicit C#/.NET class for it but (I assume) it just falls back to WIC and lets Windows resolve the decoder. All decoders are native implementations just some have explicit wrappers in .NET - but if you look at their implementation they all do the same as the UnknownBitmapDecoder and forward to base class
So the fact that there is no named class for it doesn't really mean anything.
@grubioe /cc @vatsan-madhavan can we get that reevaluated? ~If this is a bug in WIC or (external to WPF) WEBP decoder it would be really annoying if its not forwarded to the right Windows team just because the WPF team planned it for "future"~
[edit] its a bug in WPF but it'd still be nice to reevaluate and fix in 5.0 timeframe instead of leaving it on "future"
The thumbnail appears correctly on Windows explorer. I don't know if it ends up using the same decoder though.
Unless someone beats me to it I'll test with native WIC when I get home to determine if this is a WPF bug or WIC bug, for some reason my Windows 10 / 1909 installation at work doesn't have the webp decoder (I get an exception when referencing the image in WPF) and Windows doesn't offer it as optional feature either. ~I'm pretty sure I've seen it yesterday on my home machine when I happened to scroll through optional features.~
[edit] apparently its not an optional feature, you can find it under "apps and features" as installed application but it isn't uninstallable. ~Still no idea how to install webp support on my work machine, would have assumed it gets installed automatically with the 1903 or 1909 update.~
[edit] apparently you can install it from the store
The bug is in WPF, probably in native code which hasn't been open sourced yet. WIC can load the image just fine, test code to load the image via WIC attached.
In fact I also figured out what happens, if you call new BitmapImage(new Uri(...)).Format
on the webp image it reports Bgr32
which is wrong. WPF simply ignores the alpha channel even though WIC reports that the image has an alpha channel. The webp image has color artifacts in transparent places so it looks weird when they aren't transparent. (Its probably an optimization of the webp encoding because color in transparent regions is irrelevant so it doesn't bother to encode it and just leaves it at whatever is convenient.)
@arpitmathur can we bring this one up in our bug triage again for discussion ?
More than just WebP, I’d like us to think about how we are going to support emerging media formats for which native WIC support is lacking. We may want to find out how Office does this today, for e.g. to see if their approach is reusable for us.
{It could be that we wait till Jan to dig into this.}
Would be nice to have native support for .gif. I usually use https://github.com/XamlAnimatedGif/WpfAnimatedGif for this.
我也发现了 这个 bug ,怎么处理 !??
@icetech233 See https://github.com/dotnet/wpf/issues/1436#issuecomment-562269664 for a workaround. Basically you need to convert the image into a known pixel format. You can let WIC do it:
var decoder = BitmapDecoder.Create(new Uri("1_webp_a.webp", UriKind.Relative), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
var converted = new FormatConvertedBitmap(decoder.Frames[0], PixelFormats.Bgra32, null, 0);
Content = new Image { Source = converted };
The WebP decoder returns GUID_WICPixelFormat32bppRGBA
which is not supported by WPF so it fallbacks to Default
. I suggested extending the supported pixel formats in #4570
But I also agree with @vatsan-madhavan we need an easy way to plug in managed only/non-WIC image decoders and encoders.
Based on the example in learn.microsoft I coded the following xaml:
<Window x:Class="WpfApplicationForStackOverflow1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationForStackOverflow1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Background="Aquamarine">
<StackPanel>
<Image Name="Image1" Width="400">
<Image.Source>
<BitmapImage DecodePixelWidth="997"
UriSource="https://www.gstatic.com/webp/gallery3/1_webp_ll.webp" />
</Image.Source>
</Image>
</StackPanel>
</Window>
It works. The background is transparent. The DecodePixelWidth seems to have a healthy effect. Unfortunately not for every value. I prefer prime numbers like 997.
Problem description:
WebP can now be displayed on Windows 10, but when trying to display transparent WebP with WPF, the transparent part becomes broken display.
However, BitmapImage using DecodePixelWidth different from the source is displayed correctly.
I submitted a similar report as an issue with the .NET Framework.
Minimal repro:
<Image Source="1_webp_a.webp"/>
only.