19

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

Tim
  • 1,749
  • 3
  • 24
  • 48

6 Answers6

19

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>();
Tim
  • 1,749
  • 3
  • 24
  • 48
  • Will that support Displaying an animated gif ? – smohamed Nov 09 '11 at 00:10
  • 1
    I can't seem to get this to work, the code compiles and all but during runtime initialization throws exception about incompatible binding type. Since this post is about a year old, has the ImageTools library been updated in a way that would be incompatible with this? – Esko Dec 04 '11 at 09:42
  • i'm using same concept for display gif image.but, some time i'm getting this error "The type initializer for 'ImageTools' threw an exception.".. if u know this error help me. – Jeeva Feb 24 '12 at 09:20
  • It does't work in my case at all, It is no image(http://stackoverflow.com/questions/9667840/how-to-use-gif-animated-image-in-wp-7) – revolutionkpi Mar 12 '12 at 14:18
  • Thank you for this valuable information. I had to add this.DataContext = this; for it to work in my solution (in case other people are struggeling) :) – Svein Erik Jan 24 '13 at 18:38
3

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

Bil Simser
  • 1,713
  • 1
  • 19
  • 27
  • i'm using same concept for display gif image.but, some time i'm getting this error "The type initializer for 'ImageTools' threw an exception.".. if u know this error help me. – Jeeva Feb 24 '12 at 09:20
2

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

Dan
  • 2,212
  • 20
  • 29
1

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

Mark
  • 14,820
  • 17
  • 99
  • 159
1

WP7 Silverlight supports JPG/PNG.

Mick N
  • 14,892
  • 2
  • 35
  • 41
1

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.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • I just checked the link you gave and it says that GIF is supported on all devices (now). – T3rm1 May 31 '12 at 07:09
  • 1
    @T3rm1 well, that's potentially confusing. The OS has support for those 4 formats (bmp, jpg, png & gif) so you can view them in emails or on web pages. HOWEVER, Silverlight only supports JPG & PNG formats (See remarks http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapimage(v=vs.95).aspx). This means that you cannot GIFs directly in your code. – Matt Lacey May 31 '12 at 08:56