How to display a GIF from a URL link

Jan 22, 2012 at 5:38 PM

Hi,

I have an issue  where I'm trying to load a GIF from a link passed to me in an RSS feed. Unlike a lot of the examples on here I am trying to do this through a listbox item when it is databound.

Currently I have the following in my XAML:

 

xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
....
<ListBox x:Name="Weather">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>

                            <imagetools:AnimatedImage Source="{Binding WeatherIcon, Converter={StaticResource DisplayGIF}}" />

                            <TextBlock Name="temperatureBlock" Text="{Binding WeatherTemperatureSummary}" TextWrapping="Wrap" Margin="12,0,0,0" FontSize="{Binding HeadlineSize}" Foreground="{Binding WeatherTemperatureSummary, Converter={StaticResource StylesAndColours}}" />
                            <TextBlock Name="summaryBlock" Text="{Binding Summary, Converter={StaticResource RssTextTrimmer}}" TextWrapping="Wrap" Margin="12,-6,0,10" FontSize="{Binding SummarySize}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

 

 

When the imagetools:AnimatedImage tag is databound it calls a class in my project DisplayGIF which is below:

using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Gif;
using System.Windows.Data;

public class DisplayGIF : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ExtendedImage image = new ExtendedImage();
            ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
            image.UriSource = new Uri(value.ToString(), UriKind.Absolute);
            return image.ToBitmap();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

 

The above results in a 'Image not loaded' message. I know I have to wait for the image to be downloaded from the link but I'm not sure how to go about this?

I can see in your examples that you use 'image_LoadingCompleted' but it's not clear what I am meant to put in there to return a bitmap.

Any help would be appreciated. I currently have this question on Stackoverflow.com which I will update once I get it working :)

http://stackoverflow.com/questions/8960970/how-do-i-display-a-gif-on-windows-phone-when-calling-a-staticresource

Thanks,

Rich

Jan 23, 2012 at 8:07 AM

ok, turns out that the images I was trying to link to were not Hot Link-able! so code was working but image source was denying me access!

A great code sample is over here for anyone interested:

http://www.java2s.com/Open-Source/ASP.NET/Silverlight/imagetools/ImageTools/Controls/ImageConverter.cs.htm