static void ImageControlUnloaded(object sender, RoutedEventArgs e) { Image imageControl = sender as Image; if (imageControl == null) { return; } var source = GetAnimatedSource(imageControl); if (source != null) { AnimationCache.DecrementReferenceCount(source, GetRepeatBehavior(imageControl)); } var controller = GetAnimationController(imageControl); if (controller != null) { controller.Dispose(); } }
private static async Task AnimatedSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { Image imageControl = o as Image; if (imageControl == null) { return; } var oldValue = e.OldValue as ImageSource; var newValue = e.NewValue as ImageSource; if (ReferenceEquals(oldValue, newValue)) { return; } if (oldValue != null) { imageControl.Loaded -= ImageControlLoaded; imageControl.Unloaded -= ImageControlUnloaded; AnimationCache.DecrementReferenceCount(oldValue, GetRepeatBehavior(imageControl)); var controller = GetAnimationController(imageControl); if (controller != null) { controller.Dispose(); } imageControl.Source = null; } if (newValue != null) { imageControl.Loaded += ImageControlLoaded; imageControl.Unloaded += ImageControlUnloaded; if (imageControl.IsLoaded) { await InitAnimationOrImage(imageControl); } } }
private static async Task RepeatBehaviorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { Image imageControl = o as Image; if (imageControl == null) { return; } ImageSource source = GetAnimatedSource(imageControl); if (source != null) { if (!Equals(e.OldValue, e.NewValue)) { AnimationCache.DecrementReferenceCount(source, (RepeatBehavior)e.OldValue); } if (imageControl.IsLoaded) { await InitAnimationOrImage(imageControl); } } }
private static async Task <ObjectAnimationUsingKeyFrames> GetAnimation(Image imageControl, BitmapSource source) { var animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl)); if (animation != null) { return(animation); } var result = await GetDecoder(source); var decoder = result.decoder as GifBitmapDecoder; var gifMetadata = result.gifFile; if (decoder != null && decoder.Frames.Count > 1) { var fullSize = GetFullSize(decoder, gifMetadata); int index = 0; animation = new ObjectAnimationUsingKeyFrames(); var totalDuration = TimeSpan.Zero; BitmapSource baseFrame = null; foreach (var rawFrame in decoder.Frames) { var metadata = GetFrameMetadata(decoder, gifMetadata, index); var frame = MakeFrame(fullSize, rawFrame, metadata, baseFrame); var keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration); animation.KeyFrames.Add(keyFrame); totalDuration += metadata.Delay; switch (metadata.DisposalMethod) { case FrameDisposalMethod.None: case FrameDisposalMethod.DoNotDispose: baseFrame = frame; break; case FrameDisposalMethod.RestoreBackground: if (IsFullFrame(metadata, fullSize)) { baseFrame = null; } else { baseFrame = ClearArea(frame, metadata); } break; case FrameDisposalMethod.RestorePrevious: // Reuse same base frame break; } index++; } animation.Duration = totalDuration; animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation); AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl)); return(animation); } return(null); }