Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GifImage : Image
- {
- GifBitmapDecoder _gf;
- Int32Animation _anim;
- bool _animationIsWorking = false;
- public Uri Uri
- {
- set
- {
- _gf = new GifBitmapDecoder(value, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
- _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))));
- _anim.RepeatBehavior = RepeatBehavior.Forever;
- Source = _gf.Frames[0]; ;
- }
- }
- public int FrameIndex
- {
- get { return (int)GetValue(FrameIndexProperty); }
- set { SetValue(FrameIndexProperty, value); }
- }
- public static readonly DependencyProperty FrameIndexProperty =
- DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));
- static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
- {
- var ob = obj as GifImage;
- ob.Source = ob._gf.Frames[(int)ev.NewValue];
- ob.InvalidateVisual();
- }
- protected override void OnRender(DrawingContext dc)
- {
- base.OnRender(dc);
- if (_animationIsWorking) return;
- BeginAnimation(FrameIndexProperty, _anim);
- _animationIsWorking = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment