WinRT - Display animated GIF in a control

倖福魔咒の 提交于 2019-12-05 10:25:26

While I haven't checked if it works and it might not work for you due to airspace issues - you could try hosting your gif in a WebView control. It might be a lot easier than the otherwise better solutions proposed by Norbert and Carl.

You can achieve this natively by using the BitmapDecoder class in the .NET Framework. I have an example on my blog, that shows how to implement an AnimationControl that is able to show animated GIFs from a ressource gif file. Check out: http://www.henrikbrinch.dk/Blog/2013/02/21/Windows-8---GIF-animations--the-RIGHT-way

I just released a library to play animated GIFs: XamlAnimatedGif. You just need to set an attached property on a standard Image control:

<Image gif:AnimationBehavior.SourceUri="/Images/animated.gif" />

(the xmlns mapping is xmlns:gif="using:XamlAnimatedGif")

In works on WPF, Windows 8.1 and Windows Phone 8.1

This can't be natively done but you can check out

http://imagetools.codeplex.com/

which does all you want. Also you can check this

http://blogs.msdn.com/b/jstegman/archive/2009/09/08/silverlight-3-sample-updates.aspx

which contains a GIF Decoder library

Here You have a good tutorial how to insert gif into Metro Style App:

http://advertboy.wordpress.com/2012/05/08/animated-gifs-in-xamlc/

But simplest way is use this project:

http://imagetools.codeplex.com/

As @Filip Skakun said WebView method works. Sure it's "hack" but all these libraries and decoding on fly is slow, animation flickers (at least in windows phone). Using WebView gives you ability to host animated gifs with transparent background. To use WebView gif rendering approach create html page in your project:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>gif rendering</title>
    <script type="text/javascript">
        function SetImageSource(image, source) {
            document.getElementById(image).src = source;
        };
    </script>

<style type="text/css">
    html, body
    {
        -ms-content-zooming:none;
        -ms-overflow-style: none;
        -ms-scroll-translation: none;
        -ms-touch-select: none;
        overflow: hidden;
        zoom: 100%;
    }
</style>
</head>
<body>
<img id="gifPlaceholder" src=""/>

<script>
    SetImageSource("gifPlaceholder", window.top.location.search.substring(1));
</script>
</body>
</html>

CSS is important - because it disables zoom / scrolling / touch-move of WebView (which isn't desirable in most cases). Add animated gifs to same folder (where html sits). Xaml code:

<WebView Name="gifRendererWebView" 
         Source="ms-appx-web:///pathToHtmlInProject.html?gifName.gif"
         DefaultBackgroundColor="Transparent"/>

The only drawback is that you "lose gestures" on area which WebView occupy (and you can't use WebViewBrush because you would lose animation).

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