private static void CloseAnimationOrImage(Image?imageControl) { if (imageControl == null) { return; } ImageSource source = GetAnimatedSource(imageControl); if (source != null) { AnimationCache.DecrementReferenceCount(source, GetRepeatBehavior(imageControl)); } ImageAnimationController controller = GetAnimationController(imageControl); if (controller != null) { controller.Dispose(); } SetAnimationController(imageControl, null); SetIsAnimationLoaded(imageControl, false); }
private static ObjectAnimationUsingKeyFrames?GetAnimation(Image imageControl, BitmapSource source) { ObjectAnimationUsingKeyFrames?animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl)); if (animation != null) { return(animation); } GifFile?gifMetadata; var decoder = GetDecoder(source, out gifMetadata) as GifBitmapDecoder; if (decoder != null && decoder.Frames.Count > 1) { Int32Size fullSize = GetFullSize(decoder, gifMetadata); int index = 0; animation = new ObjectAnimationUsingKeyFrames(); TimeSpan totalDuration = TimeSpan.Zero; BitmapSource?baseFrame = null; foreach (BitmapFrame rawFrame in decoder.Frames) { FrameMetadata metadata = GetFrameMetadata(decoder, gifMetadata, index); BitmapSource 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); AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation); return(animation); } return(null); }
private static void InitAnimationOrImage(Image?imageControl) { if (imageControl == null) { return; } var source = GetAnimatedSource(imageControl) as BitmapSource; bool isInDesignMode = DesignerProperties.GetIsInDesignMode(imageControl); bool animateInDesignMode = GetAnimateInDesignMode(imageControl); bool shouldAnimate = !isInDesignMode || animateInDesignMode; // For a BitmapImage with a relative UriSource, the loading is deferred until // BaseUri is set. This method will be called again when BaseUri is set. bool isLoadingDeferred = IsLoadingDeferred(source); if (source != null && shouldAnimate && !isLoadingDeferred) { // Case of image being downloaded: retry after download is complete if (source.IsDownloading) { EventHandler?handler = null; handler = (sender, args) => { source.DownloadCompleted -= handler; InitAnimationOrImage(imageControl); }; source.DownloadCompleted += handler; imageControl.Source = source; return; } ObjectAnimationUsingKeyFrames?animation = GetAnimation(imageControl, source); if (animation != null) { if (animation.KeyFrames.Count > 0) { // For some reason, it sometimes throws an exception the first time... the second time it works. TryTwice(() => imageControl.Source = (ImageSource)animation.KeyFrames[0].Value); } else { imageControl.Source = source; } RepeatBehavior repeatBehavior = GetRepeatBehavior(imageControl); bool synchronized = GetSynchronizedBySource(imageControl); AnimationCache.IncrementReferenceCount(source, repeatBehavior); AnimationClock?clock; if (synchronized) { clock = AnimationCache.GetClock(source, repeatBehavior); if (clock == null) { clock = animation.CreateClock(); AnimationCache.AddClock(source, repeatBehavior, clock); } } else { clock = animation.CreateClock(); } var controller = new ImageAnimationController(imageControl, animation, clock); SetAnimationController(imageControl, controller); SetIsAnimationLoaded(imageControl, true); imageControl.RaiseEvent(new RoutedEventArgs(AnimationLoadedEvent, imageControl)); return; } } imageControl.Source = source; }