在Windows Phone中如何显示GIF图片 已翻译 100%

曾沙 投递于 2012/11/27 16:15 (共 3 段, 翻译完成于 11-27)
阅读 453
收藏 0
0
加载中

Some time ago I was talking with a friend that needed a special requirement for his application: displaying GIF images downloaded from the web. This image format is not supported by Silverlight nor Windows Phone: if you try to download and display a GIF image inside an Image control you’ll get an exception.

So I searched a little bit on the Internet to find a possible solution and I came out withImageTools, a third party library that contains many converters and tools to convert one image from a format to another on the fly. One of the supported scenario is GIF images conversion (that can be animated, too) for Windows Phone.

Let’s see how to use it: the first thing is to add the library to your project and, as usual, the easiest way to do is with NuGet.

已有 1 人翻译此段
我来翻译
To use this library to achieve our goal we need to use two components:

  • The first is a control called AnimatedImage, which is going to replace the standard Silverlight Imagecontrol. It’s the container that will display the image.
  • The second is a converter called ImageConverter, which takes care of converting the image from one format to another.

Both objects are part of the ImageTools.Controls namespace, that should be declared in the XAML page where we’re going to use them.

xmlns:imagetools=”clr-namespace:ImageTools.Controls;assembly=ImageTools.Control”

Once you have imported the namespace, you can add the converter as a resource for the page or, if you prefer, for the global application. Here is an example on how to do it for a single page:

<phone:PhoneApplicationPage.Resources>
    <imagetools:ImageConverter x:Key="ImageConverter" />
</phone:PhoneApplicationPage.Resources>

If you want to make this resource globally available, you just have to declare it inside the Application.Resources section inside the App.xaml file.

已有 1 人翻译此段
我来翻译
Now you are ready to insert the  AnimatedImage control into your page: you simply have to bind the  Source property with a  Uri object (which contains the URL of the gif image) and apply the  Image converter.

Here is an example of the XAML declaration:


 
 
<StackPanel>
    <imagetools:AnimatedImagex:Name="Image"Source="{Binding Path=ImageSource, Converter={StaticResource ImageConverter}}"/>
</StackPanel>

And here, instead, is how the ImageSource property is defined in the code:

ImageSource = new Uri("http://www.nonstopgifs.com/animated-gifs/games/games-animated-gif-002.gif", UriKind.Absolute);

We’re almost done: the trick to make the “magic” working is to use one of the decoders that are available in the ImageTools library: these decoders take care of the conversion process and they should be initialized when the application or the page is created, specifying which is the image format to use.  We are using this library for GIF conversion, so here is how to register the GIF decoder.

public MainPage()
{
    InitializeComponent();
    ImageTools.IO.Decoders.AddDecoder();
}
As I anticipated in the beginning of this post, this control supports animated GIFs too: if you try the example GIF used in this post you can see it by yourself.
已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(0)

返回顶部
顶部