How to use an animated gif?I was wondering if there was a way to play an animated gif?  I tried setting the source of an image to a gif file but it only loads the first frame of the gif and that's it.© 2009 Microsoft Corporation. All rights reserved.Tue, 13 Oct 2009 16:06:08 Z93d50a97-0d8d-4b18-992e-cd3200693337http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#93d50a97-0d8d-4b18-992e-cd3200693337http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#93d50a97-0d8d-4b18-992e-cd3200693337Derek Juhttp://social.msdn.microsoft.com/Profile/en-US/?user=Derek%20JuHow to use an animated gif?I was wondering if there was a way to play an animated gif?  I tried setting the source of an image to a gif file but it only loads the first frame of the gif and that's it.Tue, 28 Mar 2006 00:04:12 Z2006-03-28T18:38:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#c5fc826c-7287-48bf-9074-e2c87bd0e4c1http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#c5fc826c-7287-48bf-9074-e2c87bd0e4c1R Gohttp://social.msdn.microsoft.com/Profile/en-US/?user=R%20GoHow to use an animated gif?<p>Unfortunately for V1, we have not automatic way of animating a GIF. This is something we are looking at fixing for the next version.</p> <p>One workaround would be animate the different frames manually.</p>Tue, 28 Mar 2006 06:39:17 Z2006-03-28T18:37:33Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#be835df7-4cbe-43b7-9a50-92c32641b7e2http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#be835df7-4cbe-43b7-9a50-92c32641b7e2Derek Juhttp://social.msdn.microsoft.com/Profile/en-US/?user=Derek%20JuHow to use an animated gif?Hm, ok.  Do you have any suggestions on how I can go about animating manually?Tue, 28 Mar 2006 16:54:23 Z2006-03-28T16:54:23Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#3b7e70e6-a1cb-41b7-a76e-f6cf509b4c04http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#3b7e70e6-a1cb-41b7-a76e-f6cf509b4c04Rahul Patil [MSFT]http://social.msdn.microsoft.com/Profile/en-US/?user=Rahul%20Patil%20%5bMSFT%5dHow to use an animated gif?<p>Another really simple workaround is to use MediaElement, and set the source of the MediaElement to the gif file.</p> <p>    &lt;MediaElement Source=&quot;animated.gif&quot;/&gt;</p> <p> </p> <p>-Thanks</p> <p>Rahul Patil</p>Tue, 28 Mar 2006 18:27:31 Z2006-03-28T18:38:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#869c7429-dbb8-4039-95c8-05b2b97850cahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#869c7429-dbb8-4039-95c8-05b2b97850cachongqinghttp://social.msdn.microsoft.com/Profile/en-US/?user=chongqingHow to use an animated gif?<p>Have anyone tried this?</p> <p>It seems mediaElement won't show animated gif either.</p> <p>this is my code.</p><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>MediaElement</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>x:Name</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>loadingGif</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>50</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>100</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Source</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>pack://siteoforigin:,,,/images/loading.gif</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>HorizontalAlignment</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>Center</font><font size=2>&quot;</font><font color="#0000ff" size=2>&gt;&lt;/</font><font color="#800000" size=2>MediaElement</font><font color="#0000ff" size=2>&gt;</p></font> <p>thanks/chong</p>Wed, 29 Mar 2006 22:09:12 Z2006-03-29T22:09:12Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#28d42358-17f9-48bf-9033-93094d5ef0echttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#28d42358-17f9-48bf-9033-93094d5ef0ecDennis Cheng - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Dennis%20Cheng%20-%20MSFTHow to use an animated gif?<p>The MediaElement doesn't support the pack:// scheme yet. Try a relative or absolute URI such as http:// or file://. It downloads and plays the GIF just fine.</p>Wed, 29 Mar 2006 22:51:09 Z2006-03-29T22:51:09Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#28f17e32-6636-4f74-89b8-a09434962d49http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#28f17e32-6636-4f74-89b8-a09434962d49Derek Juhttp://social.msdn.microsoft.com/Profile/en-US/?user=Derek%20JuHow to use an animated gif?<p>For some reason I still cannot get this to work :(.</p> <p>Here is all I am doing:</p> <p>&lt;MediaElement Source=&quot;animation.gif&quot;&gt;</p> <p>I have the animation.gif as an item in my project.  When I load up my Window however, I just see a blank space.  The link is correct because if I switch MediaElement with Image I see the first frame of my gif.</p>Sat, 01 Apr 2006 00:01:04 Z2006-04-01T00:01:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#5be0ed06-2c4c-491a-b9f5-155f6f5a0242http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#5be0ed06-2c4c-491a-b9f5-155f6f5a0242Dennis Cheng - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Dennis%20Cheng%20-%20MSFTHow to use an animated gif?That relative path is shorthand for &quot;pack://application:,,,/animation.gif&quot; which doesn't work. The best way to refer to media today is to use an absolute URI to a loose file outside of the project.Sat, 01 Apr 2006 00:12:42 Z2006-04-01T00:12:42Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#1ec8db2f-3c0e-4327-bbe0-c1b7e9291794http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#1ec8db2f-3c0e-4327-bbe0-c1b7e9291794csammishttp://social.msdn.microsoft.com/Profile/en-US/?user=csammisHow to use an animated gif?<p>I'm using a MediaElement to animate a GIF successfully, in this way:</p> <p>&lt;MediaElement LoadedBehavior=&quot;Play&quot; Source=&quot;<a title="file:///C:/anim.gif">file://C:/anim.gif</a>&quot; /&gt;</p> <p>However, when the GIF has transparency, the MediaElement shows black in the areas that should be transparent...ostensibly, the transparent areas of the GIF would show the underlying control.  How can I change this?</p> <p>Also, I don't really like the idea of using a MediaElement for the task of animating small GIFs, such as animating emoticons for use in an IM application.  I feel that it's way too heavy an object to create and destroy frequently.  Does anyone have details on the frame-by-frame animation method?</p>Sun, 02 Apr 2006 04:31:57 Z2006-04-02T04:31:57Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#429c1ff9-0ffe-4319-a903-39587f37218bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#429c1ff9-0ffe-4319-a903-39587f37218bJared Bienzhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jared%20BienzHow to use an animated gif?<P>This article shows you how to enumerate through each frame in the animated gif file and obtain them as System.Drawing.Bitmap instances:</P> <P><a target="_blank" title="http://www.eggheadcafe.com/articles/stripimagefromanimatedgif.asp" href="http://www.eggheadcafe.com/articles/stripimagefromanimatedgif.asp">http://www.eggheadcafe.com/articles/stripimagefromanimatedgif.asp</a></P> <P>This thread shows how to convert a System.Drawing.Bitmap into a BitmapSource, which can be displayed in the WPF Image control:</P> <P><a target="_blank" title="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=320846&amp;SiteID=1" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=320846&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=320846&amp;SiteID=1</a></P> <P>I would think you could leverage this to create&nbsp;a user control based on the WPF Image control and a timer. The only thing I'm not sure of is how to&nbsp;determine how long you should pause between each frame. That information is of course stored in the gif file, but I'm not sure if you can obtain it through .Net.</P> <P>Unfortunately, I don't really have an idea how slow this would be. I'd venture to guess that the conversion of a Drawing.Bitmap to BitmapSource is a fairly costly process. You would probably want to convert all frames of the gif to BitmapSources as you load the gif file, and then cache them so you don't have to convert on each frame draw.</P> <P>Hope that helps...</P> <P>Jared</P>Sun, 02 Apr 2006 17:09:13 Z2006-04-02T17:09:13Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#3b7a3f1e-c903-4e1a-9143-27abd6c070d1http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#3b7a3f1e-c903-4e1a-9143-27abd6c070d1d00dhttp://social.msdn.microsoft.com/Profile/en-US/?user=d00dHow to use an animated gif?Has the support for animated GIFs using Image been provided in the latest versions of WPF?<br>Mon, 23 Oct 2006 10:50:23 Z2006-10-23T10:50:23Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#d866a034-0132-4348-b8c9-11935b0c17cdhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#d866a034-0132-4348-b8c9-11935b0c17cdNewYodahttp://social.msdn.microsoft.com/Profile/en-US/?user=NewYodaHow to use an animated gif?I'd also like to use animated GIFs. <br>I tried:<br>&lt;Grid&gt;<br>        &lt;MediaElement Source=&quot;D:\Animation.gif&quot; /&gt;<br>&lt;/Grid&gt;<br>However, my Window is blank. I also tried to use Width and Height properties, but that didn't solve my problem.<br>Tue, 28 Nov 2006 21:07:01 Z2006-11-28T21:07:01Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#2e237dc6-b45a-4cd2-b63c-ac145620f90ehttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#2e237dc6-b45a-4cd2-b63c-ac145620f90eRobert A. Wlodarczykhttp://social.msdn.microsoft.com/Profile/en-US/?user=Robert%20A.%20WlodarczykHow to use an animated gif?Image element does not support Animated GIFs in this version. Some people have had luck using the MediaElement for Animated GIFs, but no guarantee is made about this solution.Thu, 30 Nov 2006 17:17:16 Z2006-11-30T17:17:16Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#1d68509d-ff11-4a85-a4d3-1d02648499b3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#1d68509d-ff11-4a85-a4d3-1d02648499b3Rob Reisshttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20ReissHow to use an animated gif?<p>As a learning project I created a WPF control to display animated GIF files.  The &quot;ImageAnim&quot; control mimics most of the behavior of Microsoft's WPF &quot;Image&quot; control with the additional benefit of handling animated GIF's.  You can download ImageAnim.dll from <a title="http://www.robreiss.com" href="http://www.robreiss.com">www.robreiss.com</a>. (its free)</p> <p>Rob Reiss</p>Mon, 26 Mar 2007 21:31:08 Z2007-03-26T21:31:08Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#0dc5f102-8da1-485f-bc20-321196fd7c86http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#0dc5f102-8da1-485f-bc20-321196fd7c86indrajit chavanhttp://social.msdn.microsoft.com/Profile/en-US/?user=indrajit%20chavanHow to use an animated gif?<p align=left>Hi All,</p> <p align=left>For me giving file:// source to mediaelement is also not working.so i opted for http as follows</p> <p align=left><br> <div class=codeseg> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <div class=codecontent>    &lt;MediaElement Source=&quot;<a title="http://192.168.2.41/it/spinner_48.gif" href="http://192.168.2.41/it/spinner_48.gif">http://192.168.2.41/it/spinner_48.gif</a>&quot; x:Name=&quot;me&quot; LoadedBehavior=&quot;Play&quot; ToolTip=&quot;Loading...&quot; Canvas.Top=&quot;50&quot; UnloadedBehavior=&quot;Play&quot; MediaFailed=&quot;me_MediaFailed&quot;&gt;&lt;/MediaElement&gt;<br>        &lt;TextBlock x:Name=&quot;txtError&quot;&gt;&lt;/TextBlock&gt;<br><br>        </div></div> <p></p> <p align=left> </p> <p align=left>but strange thing is it works in IE7 and fails in IE6. for IE6 it raises Mediafailed event and the exception is </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>&quot;media failed. system.windows.media.invalidwmpversionexception:Windows media player 10 or later is required---&gt;system.runtime.interopservices.comexception(0x88989507).Exception from HRESULT(0x88989507)</p> <p align=left>-------------------End of innerexception stack trace-------------------------&quot;</p> <p align=left> </p></div></div> <p align=left> </p> <p align=left>Above exception is caught in mediafailed event as follows:</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>private void me_MediaFailed(object sender, ExceptionRoutedEventArgs e)<br>        {</p> <p align=left>            txtError.Text = &quot;media failed &quot; + e.ErrorException.ToString();<br>        }</p> <p align=left> </p></div></div> <p align=left> </p> <p></p> <p align=left></p> <p></p> <p align=left></p> <p align=left>I agree that my client machine has windows media player 9 but how can need of WMP 10 be justified for a simple animated gif? How can i ask my client to take ie7 not ie6 and WMP 10 not 9 just for RICH UX web site(mostly containing animated gif) without video ?It really is going to embrace me</p> <p align=left> </p> <p align=left> </p></a>Mon, 12 May 2008 12:21:42 Z2008-05-12T12:21:42Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#92ac811e-7df6-4819-81da-99e76952fd6dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#92ac811e-7df6-4819-81da-99e76952fd6dolayaidahttp://social.msdn.microsoft.com/Profile/en-US/?user=olayaidaHow to use an animated gif?<br>There is a better and easier way to animate a GIF. try using winforms PictureBox with the WindowsFormsHost element. <br>The code will looks like <br><br>&lt;Window x:Class=&quot;Wing.TeleGYM.Client.Controls.StartupScreen&quot;<br>    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;<br>    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;<br>    xmlns:wfi=&quot;clr-namespace<img alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif">ystem.Windows.Forms.Integration;assembly=WindowsFormsIntegration&quot;<br>    xmlns:winForms=&quot;clr-namespace<img height=19 alt="Tongue Tied" src="http://forums.microsoft.com/MSDN/emoticons/emotion-7.gif" width=19>ystem.Windows.Forms;assembly=System.Windows.Forms&quot;<br>    Loaded=&quot;Window_Loaded&quot; &gt;<br><br>            &lt;DockPanel ... &gt;<br>                <br>                &lt;wfi:WindowsFormsHost HorizontalAlignment=&quot;Center&quot;&gt;<br>                    &lt;winForms<img height=19 alt="Stick out tongue" src="http://forums.microsoft.com/MSDN/emoticons/emotion-4.gif" width=19>ictureBox x:Name=&quot;pictureBoxLoading&quot; Width=&quot;320&quot; Height=&quot;20&quot; &gt;&lt;/winForms<img height=19 alt="Stick out tongue" src="http://forums.microsoft.com/MSDN/emoticons/emotion-4.gif" width=19>ictureBox&gt;<br>                &lt;/wfi:WindowsFormsHost&gt;<br>            &lt;/DockPanel &gt;<br><br>&lt;/Window &gt;<br><br>In the Window_Loaded function add the following line of code <br><br>pictureBoxLoading.Image = Properties.Resources.StartUp_Loading; // From resources<br><br>or <br><br>pictureBoxLoading.Image = c:\loading.gif // from dis<br><br>hope this will help<br>Mon, 19 May 2008 14:39:20 Z2008-05-19T14:39:20Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#a85d4d06-3a99-4452-8c57-9cbd9b8bb90chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#a85d4d06-3a99-4452-8c57-9cbd9b8bb90cSergey Ukrainehttp://social.msdn.microsoft.com/Profile/en-US/?user=Sergey%20UkraineHow to use an animated gif?<span class=parameter>To solve the problem with playing animated GIFs I have created control derived from System.Windows.Controls.Image. This control uses System.Drawing.ImageAnimator class for animating GIF. The main functionality of class is the following:<br><br>using System;<br>using System.Drawing;<br>using System.Windows;<br>using System.Windows.Interop;<br>using System.Windows.Threading;<br>using System.Windows.Media;<br>using System.Windows.Media.Imaging;<br>using IpmPlayerLogWriter;<br><br>public class GIFImageControl : System.Windows.Controls.Image<br>{<br>    delegate void OnFrameChangedDelegate();   <br>    private Bitmap _Bitmap;<br><br>    public AnimatedImageControl(string path)<br>    {<br>            _Bitmap = (Bitmap)Bitmap.FromFile(path);<br>            ImageAnimator.Animate(_Bitmap, new EventHandler(OnFrameChanged));<br>    }<br><br><br>    private void OnFrameChanged(object sender, EventArgs e)<br>    {<br>        Dispatcher.BeginInvoke(DispatcherPriority.Normal,<br>            new OnFrameChangedDelegate(OnFrameChangedInMainThread));<br>    }<br><br>    private void OnFrameChangedInMainThread()<br>    {<br>        ImageAnimator.UpdateFrames(_Bitmap);<br>        this.Source = this.GetBitmapSource(_Bitmap);<br>        InvalidateVisual();<br>    }<br><br>    private BitmapSource GetBitmapSource(Bitmap gdiBitmap)<br>    {<br>        return Imaging.CreateBitmapSourceFromHBitmap(<br>            gdiBitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());<br>    }<br>}<br><br>Hope that it helps :)<br></span> Wed, 04 Jun 2008 06:03:42 Z2008-06-04T06:03:42Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#c0d2ffab-d916-4119-a7ed-2928af1fdc50http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#c0d2ffab-d916-4119-a7ed-2928af1fdc50Sergey Ukrainehttp://social.msdn.microsoft.com/Profile/en-US/?user=Sergey%20UkraineHow to use an animated gif?I have found one problem in the code I have posted above. Method GetBitmapSource gives memory leak and then GDI crashes. It is better be changed that way:<br><br>    [DllImport(&quot;gdi32.dll&quot;, EntryPoint = &quot;DeleteObject&quot;)]<br>    public static extern IntPtr DeleteObject(IntPtr hDc);<br><br>    private BitmapSource GetBitmapSource(Bitmap gdiBitmap)<br>    {<br>            IntPtr hBitmap = gdiBitmap.GetHbitmap();<br>            bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, <br>                                                                 IntPtr.Zero, <br>                                                                 Int32Rect.Empty, <br>                                                                 BitmapSizeOptions.FromEmptyOptions());<br>            DeleteObject(hBitmap);<br>    }<br><br><br> Wed, 04 Jun 2008 07:18:10 Z2008-06-04T07:18:10Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#93573bf8-0db7-4730-81f7-78fb217c32cfhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#93573bf8-0db7-4730-81f7-78fb217c32cfGreg Wilsonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Greg%20WilsonHow to use an animated gif?This thread is great.  I used Sergey Ukraine's info here and made a downloadable control that will display animated .gif from either the filesystem, or from an embedded resource in the assembly.  Additionally, I added the functionality to Play/Pause the animation by clicking on the .gif.  (On by default, but can be turned off with <span style="font-size:10pt;font-family:Courier New">AllowClickToPause</span> to <span style="font-size:10pt;color:blue;font-family:Courier New">false</span><span style="font-size:10pt;font-family:Courier New">.)<br><br>I posted the <a href="http://www.solidrockstable.com/blogs/PragmaticTSQL/Lists/Posts/Post.aspx?ID=37">code on my blog</a>, where the whole downloadable project can be <a href="http://www.solidrockstable.com/blogs/PragmaticTSQL/Lists/Posts/Post.aspx?ID=37">downloaded</a>.<br><br></span><span style="font-size:10pt;color:blue;font-family:Courier New"></span> <hr class="sig">The Pragmatic TSQL ProgrammerFri, 20 Mar 2009 22:06:03 Z2009-03-20T22:06:03Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#f80cd191-76db-499a-bf9e-9cb6e8e440a0http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#f80cd191-76db-499a-bf9e-9cb6e8e440a0jesus romerohttp://social.msdn.microsoft.com/Profile/en-US/?user=jesus%20romeroHow to use an animated gif?<p>Hi<br/><br/>I had the same problem, so I created a new control, deriving it from the Image class, and applying an animation to the image with a dependency property called FrameIndex. I guess that the frame rate is 10 frames per second.<br/><br/>This control works well when you are adding the control manually to a container. Some modifications are be required to use it from XAML (using a parameterless constructor).<br/><br/>The control accepts any URI (I tried with packed URIs) in the constructor.<br/><br/>class GifImage : Image {</p> <p>    public int FrameIndex {<br/>        get { return (int)GetValue(FrameIndexProperty); }<br/>        set { SetValue(FrameIndexProperty, value); }<br/>    }</p> <p>    public static readonly DependencyProperty FrameIndexProperty =<br/>        DependencyProperty.Register(&quot;FrameIndex&quot;, typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));</p> <p>    static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) {<br/>        GifImage ob = obj as GifImage;<br/>        ob.Source = ob.gf.Frames[(int)ev.NewValue];<br/>        ob.InvalidateVisual();<br/>    }<br/>    GifBitmapDecoder gf;<br/>    Int32Animation anim ;            <br/>    public GifImage(Uri uri) {<br/>        gf = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);<br/>        anim = new Int32Animation(0, gf.Frames.Count - 1, new Duration(new TimeSpan(0,0, 0,gf.Frames.Count/10,(int)((gf.Frames.Count/10.0-gf.Frames.Count/10)*1000))));<br/>        anim.RepeatBehavior = RepeatBehavior.Forever;<br/>        Source = gf.Frames[0];<br/>    }<br/>    bool animationIsWorking = false;<br/>    protected override void OnRender(DrawingContext dc) {<br/>        base.OnRender(dc);<br/>        if (!animationIsWorking) {<br/>            BeginAnimation(FrameIndexProperty, anim);<br/>            animationIsWorking = true;<br/>        }<br/>    }<br/>}</p>Fri, 17 Jul 2009 14:02:21 Z2009-07-17T14:02:21Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#fa75dcd7-10ac-40c7-bf8f-33060247fd4dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#fa75dcd7-10ac-40c7-bf8f-33060247fd4dptkdbhttp://social.msdn.microsoft.com/Profile/en-US/?user=ptkdbHow to use an animated gif?Works nicely...   Thanks for this.  <br/> <br/>    Had some very marginal success using a MediaElement but the playback was horrid and drilling through the myriad of properties, clocks, storyboards, triggers, speedratios, repeatbehavior was getting more and more frustrating just to animate a 16x16 icon.<br/> <br/>   I think this is about as straightforward as is possible for a simple animation in WPF.  <br/> <br/> Only tweek I had to make was to apply &quot;Stretch=Stretch.None&quot; to keep the image at its original size when I used it as content in a button.<br/> <br/>Thu, 20 Aug 2009 13:59:51 Z2009-08-20T14:05:26Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#4e0b73f6-dce4-4a3a-8ea2-f186ba0bf11fhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#4e0b73f6-dce4-4a3a-8ea2-f186ba0bf11fjlspublichttp://social.msdn.microsoft.com/Profile/en-US/?user=jlspublicHow to use an animated gif?Jesus,<br/><br/>I like your approach since it doesn't require the image format to be retranslated from an HBitmap for every frame display the way Sergey and Greg's did.  I implemented a variation that can be used from XAML and that hides a little more of the implementation from the caller (by not having an exposed FrameIndex property).  I no habla C# so my version is in VB:<br/><br/> <pre lang=x-vbnet>Imports System.Drawing Imports System.Threading Imports Image = System.Windows.Controls.Image Public Class AnimatedGIFViewer Inherits Image Private _GIFDecoder As GifBitmapDecoder Private _FrameTimer As Timer Private _ShowingFrame As Boolean = False Private _FrameIndex As Integer Public Property GIFSource() As Uri Get Return DirectCast(GetValue(GIFSourceProperty), Uri) End Get Set(ByVal value As Uri) SetValue(GIFSourceProperty, value) End Set End Property Public Shared ReadOnly GIFSourceProperty As DependencyProperty = _ DependencyProperty.Register( _ &quot;GIFSource&quot; _ , GetType(Uri) _ , GetType(AnimatedGIFViewer) _ , New FrameworkPropertyMetadata(Nothing, AddressOf GIFSource_Changed) _ ) Private Shared Sub GIFSource_Changed(ByVal sender As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) DirectCast(sender, AnimatedGIFViewer).GIFSourceSet() End Sub Private Sub GIFSourceSet() KillCurrentTimer() If IsNothing(GIFSource) Then _GIFDecoder = Nothing Else _GIFDecoder = New GifBitmapDecoder(GIFSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default) _FrameIndex = 0 _FrameTimer = New Timer(AddressOf ShowFrame, Nothing, 200, 200) End If End Sub Private Sub ShowFrame(ByVal State As Object) 'Avoid getting flooded 'with event calls since 'the timer doesn't pay 'any attention to whether 'we are keeping up or not. If _ShowingFrame Then Exit Sub End If _ShowingFrame = True Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, New Action(AddressOf OnFrameChanged)) _ShowingFrame = False End Sub Private Sub OnFrameChanged() Source = _GIFDecoder.Frames(_FrameIndex) _FrameIndex += 1 If (_FrameIndex = _GIFDecoder.Frames.Count) Then _FrameIndex = 0 End If End Sub Private Sub KillCurrentTimer() If (Not IsNothing(_FrameTimer)) Then Dim FrameTimerDisposed As New AutoResetEvent(False) _FrameTimer.Dispose(FrameTimerDisposed) 'In order to ensure that we don't have 'two timers going at once, we will wait 'for the dispose to complete but only 'for up to 1 second. FrameTimerDisposed.WaitOne(1000) _ShowingFrame = False _FrameTimer = Nothing End If End Sub Private Sub AnimatedGIFViewer_Unloaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Unloaded KillCurrentTimer() End Sub End Class </pre> <br/>You can use it XAML as follows: <br/><br/> <pre lang=x-xml> &lt;local:AnimatedGIFViewer Height=&quot;130&quot; Width=&quot;156&quot; x:Name=&quot;Image1&quot; Stretch=&quot;None&quot; GIFSource=&quot;pack://application:,,,/MyProject;component/MyGif.gif&quot; /&gt; </pre> <br/><br/>-JLSWed, 09 Sep 2009 13:03:06 Z2009-09-09T13:03:06Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#2089127c-3bf8-49ce-a738-7dc60daf2576http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#2089127c-3bf8-49ce-a738-7dc60daf2576msp.netdevhttp://social.msdn.microsoft.com/Profile/en-US/?user=msp.netdevHow to use an animated gif?Thanks, this solution works fine, but how is possible that microsoft don't fix mediaElement control problem !?Mon, 21 Sep 2009 15:44:36 Z2009-09-21T15:44:36Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#db4c9096-cc5d-4d7c-a1d2-72fa23be2c8dhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93d50a97-0d8d-4b18-992e-cd3200693337#db4c9096-cc5d-4d7c-a1d2-72fa23be2c8dGiuseppe Ugohttp://social.msdn.microsoft.com/Profile/en-US/?user=Giuseppe%20UgoHow to use an animated gif?I would really hope Microsoft supports&nbsp;animated GIF directly in ImageTue, 13 Oct 2009 16:06:08 Z2009-10-13T16:06:08Z