I am using a RSS feed to display content in my app. Everything works great except gif images. I have read that Silverlight doesn’t support the gif file format so I’ve been trying to use the ImageTools plugin.
There seems to be loads of examples where people click a button and the image displays on the same page but I want to call the image when my listbox is being populated.
Currently this is what I have:
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>
DisplayGif class:
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Gif;
using System.Windows.Data;
using System.Text;
using System.IO;
using System.Windows.Media.Imaging;
public class DisplayGIF : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] buffer = Encoding.Unicode.GetBytes(value.ToString());
Stream stream = new MemoryStream(buffer);
ExtendedImage image = new ExtendedImage();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
return image.ToBitmap(); // give error that image is not loaded
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
The above code fails as the image is not yet loaded. Is there a work around to this or even a better/easier way to get the gif images to display?
EDIT 1
Based on Ku6opr’s answer I have altered my class. When I start my app now it hangs as I suspect I am not managing the threads correctly.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] buffer = Encoding.Unicode.GetBytes(value.ToString());
Stream stream = new MemoryStream(buffer);
ExtendedImage image = new ExtendedImage();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
image.SetSource(stream);
EventWaitHandle Wait = new AutoResetEvent(false);
image.LoadingCompleted += (s, e) =>
{
Wait.Set();
};
Wait.WaitOne();
return image.ToBitmap();
}
Edit 2:
Ok, turns out the gif images I was trying to link to were not hot linkable! The link provided by Ku6opr do work – just make sure that you can actually link to the images from an outside source 😉
Thanks,
Rich
Probably, it loads image asynchronously. I guess, you need to wait while it completely loaded.
ExtendedImagemaybe has some event (LoadingCompletedor something else)While I wrote this code I see that you never assign
Streamto your image. Check itEDIT:
ImageToolsalready hasImageConverterclass that works withStreams,AbsoluteandRevativeuri paths. It source code to look inside you can find here: http://www.java2s.com/Open-Source/ASP.NET/Silverlight/imagetools/ImageTools/Controls/ImageConverter.cs.htm