Tim
Tim

Reputation: 1769

Display GIF in a WP7 application with Silverlight

I would like to display gif in my WP7 application. Is there some way to achieve this ?

I've tryed this one http://imagetools.codeplex.com/ but can't make it working with WP7.

Thanks in advance for any help

Upvotes: 19

Views: 18478

Answers (6)

Dan
Dan

Reputation: 2302

I struggled to get the accepted answer working. The following solution worked for me to display a static gif.

    public ImageResponse(string imageUrl)
    {
        InitializeComponent();

        ImageTools.IO.Decoders.AddDecoder<GifDecoder>();

        var imageResponse = new ExtendedImage();
        imageResponse.UriSource = new Uri(imageUrl);

        imageResponse.LoadingCompleted += this.ImageResponseLoadingCompleted;
    }

    private void ImageResponseLoadingCompleted(object sender, EventArgs e)
    {
        var imageResponse = (ExtendedImage)sender;

        Classes.Util.UiThread.Invoke(() =>
            {
                this.ImageResponse.Source = imageResponse.ToBitmap();
            });
    }

Classes.Util.UiThread is a helper class I use to call the UI Thread

this.ImageResponse is a standard image control

Upvotes: 2

Bil Simser
Bil Simser

Reputation: 1723

Check out Jamie Rodriguez's post here on using GIFs with WP7. He uses the ImageTools project from CodePlex.

http://blogs.msdn.com/b/jaimer/archive/2010/11/23/working-with-gif-images-in-windows-phone.aspx

Upvotes: 3

Matt Lacey
Matt Lacey

Reputation: 65556

As per http://msdn.microsoft.com/en-us/library/ff462087(VS.92).aspx the Silverlight image control does not support GIF files.

By using ImageTools you are converting the GIF file to something else on the fly on the device. If you are using gif files that you have control of (i.e. You are bundling them in the XAP or they are coming from your webserver.) you should use converted versions of these files.

This will mean that the app has to do less.
The knock on effect is that:
1. You will have to write less code.
2. The app will have to do less work and so will perform slightly better.

Of course, this doesn't cover animated GIFs. For these you'll need to use a different approach.

Upvotes: 1

Tim
Tim

Reputation: 1769

In fact, it's working, but it lacks some documentation.

After some troubles, here's how to use it :

  • reference ImageTools
  • reference ImageTools.Controls
  • reference ImageTools.IO.Gif

Add namespace in xaml :

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

And resources :

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

Then use the control with the converter :

<imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" />

Your ImageSource should be an Uri, for example :

ImageSource = new Uri("http://mysite/my.gif", UriKind.Absolute);

Don't forget to add decoded :

ImageTools.IO.Decoders.AddDecoder<GifDecoder>();

Upvotes: 19

Mick N
Mick N

Reputation: 14882

WP7 Silverlight supports JPG/PNG.

Upvotes: 1

Mark
Mark

Reputation: 14930

Is it an animated GIF? If not, I would try converting the GIF to another supported file format before using it in your app.

Upvotes: 1

Related Questions