title: Animated Gifs with Imagetools, in WP7 c#
body: <p>Well, This has been solved. I don't know if it was a glitch on my end or if ImageTools was just a pain to set up accordingly. Thank you for the answers everyone, and they very likely all work. The once I marked as answered does indeed work, BUT HERE IS HOW:</p>
  
  <blockquote>
    <p>(Credits go to Patrick for sticking with this, and his code is used as
    noted below.)</p>
    
    <p>(Also, big thanks to everyone else too who submitted something. Sorry
    if my noobness scared you all away)</p>
  </blockquote>
  
  <p>To get an animated Gif to work in your wp7 7.1 app, do these steps:</p>
  
  <p>1) Download ImageTools (I used latest version at the time (0.3)) <a href="http://imagetools.codeplex.com/downloads/get/156530" rel="nofollow noreferrer">http://imagetools.codeplex.com/downloads/get/156530</a></p>
  
  <p>2) Unstuff the file, and in the "Bin > Phone" folder just throw ALL* the dll file extensions into your wp7 app folder. The other files (xml/pdb) don't need to be added. (*this step is extra work, and we will be removing these extra dlls later on, but hell it'll save a headache.)</p>
  
  <p>3) Add the references to your wp7 app in the Solution Explorer window > References folder drop down. To do that, right click the References folder, click "Add Reference" and browse to the dll files. Repeat this process. (Referencing all those dlls is 1 minute of the extra 2 mins of work ultimately, but you shouldn't get any errors when compiling)</p>
  
  <p>4) Now on the xaml page you want to add the image to, add this at the top in your  header code:</p>
  
  <pre><code>xmlns:it="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls"
  xmlns:vm="clr-namespace:GifViewer.ViewModels"
  </code></pre>
  
  <p>NOTICE: Change "GifViewer" to your application name.</p>
  
  <p>5) On that same page, just below it, add in this code:</p>
  
  <pre><code>&lt;phone:PhoneApplicationPage.DataContext&gt;
      &lt;vm:MainViewModel/&gt;
  &lt;/phone:PhoneApplicationPage.DataContext&gt;
  
  &lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt;
      &lt;it:AnimatedImage  Source="{Binding AnimationImage}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>NOTICE: Doesn't have to be a grid. Doesn't have to be in anything at all. It can stand alone.</p>
  
  <p>6) The accepted answer has a folder in the app called "ViewModels" and in it is a custom class titled "MainViewModel"  So in the solution explorer or in the desktop, add a folder called ViewModels and make a c# class page titled MainViewModel. Move that into that folder, and refresh the solution explorer. If you cannot see the file, you need to click the "Show all files" button just under the Solution Explorer Header.</p>
  
  <p>7) Using the accepted answer below, in the "MainViewModel.cs" class page, add the following just below the others:</p>
  
  <pre><code>using ImageTools;
  using ImageTools.Controls; 
  using ImageTools.IO;
  using ImageTools.IO.Gif;
  </code></pre>
  
  <p>8) The accepted answer uses this code. Change "GifViewer" to your application name when copying this code, and also change the Uri location of the gif. In my example, I have a folder named "Gif" and in it is "explosion.gif". Build Action can be kept as Resource by default.</p>
  
  <pre><code>namespace GifViewer.ViewModels {
      public class MainViewModel : DependencyObject {
          public MainViewModel() {
              Decoders.AddDecoder&lt;GifDecoder&gt;();
              Uri uri = new Uri("Gif/explosion.gif", UriKind.Relative);
              ExtendedImage image = new ExtendedImage();
              // either of these two method work.
              // Just remove the first / to switch
              //*
              image.LoadingCompleted +=
                  (o, e) =&gt; Dispatcher.BeginInvoke(() =&gt; AnimationImage = image);
              image.UriSource = uri;
              /*/
              Stream stream = Application.GetResourceStream(uri).Stream;
              GifDecoder decoder = new GifDecoder();
              decoder.Decode(image, stream);
              AnimationImage = image;
              /**/
          }
  
          public static readonly DependencyProperty AnimationImageProperty =
              DependencyProperty.Register("AnimationImage",
                  typeof(ExtendedImage),
                  typeof(MainViewModel),
                  new PropertyMetadata(default(ExtendedImage)));
  
          public ExtendedImage AnimationImage {
              get { return (ExtendedImage)GetValue(AnimationImageProperty); }
              set { SetValue(AnimationImageProperty, value); }
          }
      }
  }
  </code></pre>
  
  <p>Go ahead and compile. You might get a runtime error indicating that  
  couldn't load, but running the application should get rid of it.</p>
  
  <p>Your gif should now play.</p>
  
  <p>9) Start removing the EXTRA dll references which aren't needed (the other minute of the 2 mins of extra work). Basically, all you want referenced is:</p>
  
  <pre><code>ImageTools
  ImageTools.Controls
  ImageRools.IO.Gif 
  </code></pre>
  
  <p>ImageTools.Controls might not even be needed, but the file size is like 25kb and honestly I couldn't get the gif to show if I removed it. </p>
  
  <p>There you go!</p>
  
  <hr>
  
  <h2>Original Question I asked</h2>
  
  <p>I am having the worst headache trying to get my animated gif to play in my WP7 app. I simply cannot connect the dots to make this happen, despite having imagetools and viewing the current "solutions" on stackoverflow/the web. </p>
  
  <p>My problem is outlined below, but for reference I have looked at: 
  <a href="https://stackoverflow.com/questions/4152564/display-gif-in-a-wp7-application-with-silverlight">Display GIF in a WP7 application with Silverlight</a>
  and <a href="http://blog.naviso.fr/wordpress/?p=733" rel="nofollow noreferrer">http://blog.naviso.fr/wordpress/?p=733</a></p>
  
  <p>So how does one actually set this blasted thing up to display animated gifs in a wp7 app!? Question in specific -- Is this the correct code to get my animated gif to appear? If not, what below needs to be fixed?</p>
  
  <p>My animated gif file location on the phone (NOT from internet):  </p>
  
  <blockquote>
    <p>Gif/explosion.gif</p>
  </blockquote>
  
  <p>Main Xaml page:</p>
  
  <pre><code>xmlns:imagetools="clr-namespace:ImageTools.Controls;assembly=ImageTools.Controls" 
  ....
  
  &lt;phone:PhoneApplicationPage.Resources&gt;
          &lt;imagetools:ImageConverter x:Key="ImageConverter" /&gt;
  &lt;/phone:PhoneApplicationPage.Resources&gt;
  
  ....
  &lt;imagetools:AnimatedImage x:Name="animationgif" Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" /&gt;
  </code></pre>
  
  <p>and the code behind for the xaml page:</p>
  
  <pre><code>public partial class MainPage : PhoneApplicationPage
  {
   using ImageTools;
   using ImageTools.Controls;
   using ImageTools.IO.Gif;
  
   public MainPage()
      {
          InitializeComponent();
          ImageTools.IO.Decoders.AddDecoder&lt;ImageTools.IO.Gif.GifDecoder&gt;();
      }
  
  
   public void eventtofiretoshowexplosion_gif(object sender, MouseButtonEventArgs e)
   {
     // A main problem -- this code doesn't work by itself, as what is the ImageSource!?
     // It cannot be used as a variable it says.
     // animationgif.ImageSource does not work at all (not a method).
     ImageSource = new Uri("http://mysite/my.gif", UriKind.Absolute);
   }
  }
  </code></pre>
  
  <p>This has been bugging me for the past few hours, and I really could use some help with this. If there is a quick fix to this, please help out and show how it is done instead of guiding me to a page. I've seen FAR too many pages about this, and while each one claims to work, it just cannot in my app.</p>