Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the following code for loading GIFImage in a WPF app</p> <pre><code> using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; /// &lt;summary&gt; /// Class to host nornmal and Gif images and plays the animation. /// To make it work, instead of setting ImageSource, set the AnimationSourcePath property. /// &lt;/summary&gt; public class GifImage : Image { /// &lt;summary&gt; /// Dependency property to hold value for AnimationSourcePath. /// &lt;/summary&gt; public static readonly DependencyProperty AnimationSourcePathProperty = DependencyProperty.Register( "AnimationSourcePath", typeof(string), typeof(GifImage), new UIPropertyMetadata(String.Empty, AnimationSourcePathPropertyChanged)); /// &lt;summary&gt; /// Dependency property to hold value of integer animation timeline values for FrameIndex. /// &lt;/summary&gt; private static readonly DependencyProperty FrameIndexProperty = DependencyProperty.Register( "FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex))); /// &lt;summary&gt; /// Dependency property to hold value of integer animation rate which slows down animation speed if value is more than 1. /// &lt;/summary&gt; private static readonly DependencyProperty FrameRefreshRateProperty = DependencyProperty.Register( "FrameRefreshRate", typeof(int), typeof(GifImage), new UIPropertyMetadata(1, AnimationSourcePathPropertyChanged)); /// &lt;summary&gt; /// Member to hold animation timeline for integer values. /// &lt;/summary&gt; private Int32Animation anim; /// &lt;summary&gt; /// Member to hold flag to indicate if animation is working. /// &lt;/summary&gt; private bool animationIsWorking = false; /// &lt;summary&gt; /// Member to hold Gif Bitmap Decoder. /// &lt;/summary&gt; private GifBitmapDecoder gf; /// &lt;summary&gt; /// Initializes a new instance of the GifImage class. /// &lt;/summary&gt; public GifImage() { } /// &lt;summary&gt; /// Initializes a new instance of the GifImage class based on the uri. /// &lt;/summary&gt; /// &lt;param name="uri"&gt;Uri of the image source.&lt;/param&gt; public GifImage(Uri uri) { GifImage.SetupAnimationSource(this, uri); } /// &lt;summary&gt; /// Gets or sets a value indicating AnimationSourcePath. /// &lt;/summary&gt; public string AnimationSourcePath { get { return (string)GetValue(AnimationSourcePathProperty); } set { SetValue(AnimationSourcePathProperty, value); } } /// &lt;summary&gt; /// Gets or sets a value indicating FrameIndex. /// &lt;/summary&gt; public int FrameIndex { get { return (int)GetValue(FrameIndexProperty); } set { SetValue(FrameIndexProperty, value); } } /// &lt;summary&gt; /// Gets or sets a value for frame refresh rate. A value more than 1 would slow the animation down. /// &lt;/summary&gt; public int FrameRefreshRate { get { return (int)GetValue(FrameRefreshRateProperty); } set { SetValue(FrameRefreshRateProperty, value); } } /// &lt;summary&gt; /// Method to handle property changed event of AnimationSourcePath property. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;Source image.&lt;/param&gt; /// &lt;param name="ev"&gt;Event arguments.&lt;/param&gt; protected static void AnimationSourcePathPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev) { GifImage ob = obj as GifImage; ob.BeginAnimation(GifImage.FrameIndexProperty, null); ob.anim = null; ob.gf = null; ob.Source = null; if (!String.IsNullOrEmpty(ob.AnimationSourcePath) &amp;&amp; Uri.IsWellFormedUriString(ob.AnimationSourcePath, UriKind.RelativeOrAbsolute)) { if (((string)ob.AnimationSourcePath).ToLower().EndsWith(".gif")) { Uri uri = new Uri(ob.AnimationSourcePath); GifImage.SetupAnimationSource(ob, uri); ob.BeginAnimation(GifImage.FrameIndexProperty, ob.anim); } else { ob.Source = (new ImageSourceConverter()).ConvertFromString(ob.AnimationSourcePath) as ImageSource; ob.InvalidateVisual(); } ob.animationIsWorking = true; } } /// &lt;summary&gt; /// Method to handle property changed event of FrameIndex property. /// &lt;/summary&gt; /// &lt;param name="obj"&gt;Source image.&lt;/param&gt; /// &lt;param name="ev"&gt;Event arguments.&lt;/param&gt; protected static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) { GifImage ob = obj as GifImage; ob.Source = ob.gf.Frames[ob.FrameIndex]; ob.InvalidateVisual(); } /// &lt;summary&gt; /// Method to setup animation source against a Gif Image. /// &lt;/summary&gt; /// &lt;param name="ob"&gt;Gif image.&lt;/param&gt; /// &lt;param name="uri"&gt;Uri of the gif image source.&lt;/param&gt; protected static void SetupAnimationSource(GifImage ob, Uri uri) { ob.gf = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); double val = (ob.gf.Frames.Count / 15.0) - (ob.gf.Frames.Count / 15); TimeSpan tmpSpn = new TimeSpan(0, 0, 0, ob.gf.Frames.Count / 15, (int)(val * 1000)); Duration durtn = new Duration(new TimeSpan(tmpSpn.Ticks * ob.FrameRefreshRate)); ob.anim = new Int32Animation(0, ob.gf.Frames.Count - 1, durtn); ob.anim.RepeatBehavior = RepeatBehavior.Forever; ob.Source = ob.gf.Frames[0]; } /// &lt;summary&gt; /// Method to override the OnRender event of the image. /// &lt;/summary&gt; /// &lt;param name="dc"&gt;Drawing Context of the image.&lt;/param&gt; protected override void OnRender(DrawingContext dc) { base.OnRender(dc); if (!this.animationIsWorking) { BeginAnimation(GifImage.FrameIndexProperty, this.anim); this.animationIsWorking = true; } } } </code></pre> <p>The way you use it as follows....</p> <pre><code> &lt;ns:GifImage AnimationSourcePath="../MyGifImage.gif" /&gt; </code></pre> <p>Let me know if this helps you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload