使用 MediaElement 在 WPF 中制作 Gif 动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27854766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animating Gif in WPF using MediaElement
提问by Sumanth Kumar Uppala
I'm using the MediaElement to show gif image as shown below.
我正在使用 MediaElement 显示 gif 图像,如下所示。
<MediaElement x:Name="imgLoadingImage"
MediaEnded="imgLoadingImage_MediaEnded"
UnloadedBehavior="Manual"
Source="file:\loading.GIF"
LoadedBehavior="Play"
Stretch="None"
Visibility="Visible"/>
It works fine but the transparent pixels are shown as black.
它工作正常,但透明像素显示为黑色。
Is there anyway we can make it transparent?
无论如何我们可以让它透明吗?
回答by Anthony Nichols
The WebBrowseroption won't work with MVVM, however you can apply the animated Gif as it's own transparency mask and then it works:
该WebBrowser选项不适用于 MVVM,但是您可以应用动画 Gif 作为它自己的透明蒙版,然后它可以工作:
<MediaElement LoadedBehavior="Play" Source="{Binding MyGif}" >
<MediaElement.OpacityMask>
<ImageBrush ImageSource="{Binding MyGif}"/>
</MediaElement.OpacityMask>
</MediaElement>
回答by Sumanth Kumar Uppala
I did a work around for this. Instead of using MediaElement control I've used WebBrowser control which can render GIF image as HTML content
我为此做了一个工作。我没有使用 MediaElement 控件,而是使用了 WebBrowser 控件,它可以将 GIF 图像呈现为 HTML 内容
<WebBrowser x:Name="wbImage" Source="pack://siteoforigin:,,,/Images/loading.gif"/>

