Display GIF in a WP7 application with Silverlight

后端 未结 6 702
一向
一向 2020-11-27 06:10

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 workin

相关标签:
6条回答
  • 2020-11-27 06:25

    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>();
    
    0 讨论(0)
  • 2020-11-27 06:29

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

    0 讨论(0)
  • 2020-11-27 06:30

    WP7 Silverlight supports JPG/PNG.

    0 讨论(0)
  • 2020-11-27 06:30

    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.

    0 讨论(0)
  • 2020-11-27 06:32

    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

    0 讨论(0)
  • 2020-11-27 06:44

    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

    0 讨论(0)
提交回复
热议问题