DefconDotNet

GifImage

Dec 30th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1.     public class GifImage : Image
  2.     {
  3.         GifBitmapDecoder _gf;
  4.         Int32Animation _anim;
  5.         bool _animationIsWorking = false;
  6.  
  7.         public Uri Uri
  8.         {
  9.             set
  10.             {
  11.                 _gf = new GifBitmapDecoder(value, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
  12.                 _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))));
  13.                 _anim.RepeatBehavior = RepeatBehavior.Forever;
  14.                 Source = _gf.Frames[0]; ;
  15.             }
  16.         }
  17.  
  18.         public int FrameIndex
  19.         {
  20.             get { return (int)GetValue(FrameIndexProperty); }
  21.             set { SetValue(FrameIndexProperty, value); }
  22.         }
  23.  
  24.         public static readonly DependencyProperty FrameIndexProperty =
  25.             DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));
  26.  
  27.         static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
  28.         {
  29.             var ob = obj as GifImage;
  30.             ob.Source = ob._gf.Frames[(int)ev.NewValue];
  31.             ob.InvalidateVisual();
  32.         }
  33.        
  34.         protected override void OnRender(DrawingContext dc)
  35.         {
  36.             base.OnRender(dc);
  37.  
  38.             if (_animationIsWorking) return;
  39.  
  40.             BeginAnimation(FrameIndexProperty, _anim);
  41.             _animationIsWorking = true;
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment