private static void AnimatedSourceChanged( DependencyObject o, DependencyPropertyChangedEventArgs e) { if (!(o is Image imageControl)) { return; } ImageSource oldValue = e.OldValue as ImageSource; ImageSource newValue = e.NewValue as ImageSource; if (oldValue == newValue) { return; } if (oldValue != null) { imageControl.Loaded -= new RoutedEventHandler(ImageBehavior.ImageControlLoaded); imageControl.Unloaded -= new RoutedEventHandler(ImageBehavior.ImageControlUnloaded); AnimationCache.DecrementReferenceCount(oldValue, ImageBehavior.GetRepeatBehavior(imageControl)); ImageBehavior.GetAnimationController(imageControl)?.Dispose(); imageControl.Source = (ImageSource)null; } if (newValue == null) { return; } imageControl.Loaded += new RoutedEventHandler(ImageBehavior.ImageControlLoaded); imageControl.Unloaded += new RoutedEventHandler(ImageBehavior.ImageControlUnloaded); if (!imageControl.IsLoaded) { return; } ImageBehavior.InitAnimationOrImage(imageControl); }
private static void ImageControlUnloaded(object sender, RoutedEventArgs e) { if (!(sender is Image imageControl)) { return; } ImageSource animatedSource = ImageBehavior.GetAnimatedSource(imageControl); if (animatedSource != null) { AnimationCache.DecrementReferenceCount(animatedSource, ImageBehavior.GetRepeatBehavior(imageControl)); } ImageBehavior.GetAnimationController(imageControl)?.Dispose(); }
private static ObjectAnimationUsingKeyFrames GetAnimation( Image imageControl, BitmapSource source) { ObjectAnimationUsingKeyFrames animation1 = AnimationCache.GetAnimation((ImageSource)source, ImageBehavior.GetRepeatBehavior(imageControl)); if (animation1 != null) { return(animation1); } GifFile gifFile; if (!(ImageBehavior.GetDecoder(source, out gifFile) is GifBitmapDecoder decoder) || decoder.Frames.Count <= 1) { return((ObjectAnimationUsingKeyFrames)null); } ImageBehavior.Int32Size fullSize = ImageBehavior.GetFullSize((BitmapDecoder)decoder, gifFile); int frameIndex = 0; ObjectAnimationUsingKeyFrames animation2 = new ObjectAnimationUsingKeyFrames(); TimeSpan zero = TimeSpan.Zero; BitmapSource baseFrame = (BitmapSource)null; foreach (BitmapFrame frame1 in decoder.Frames) { ImageBehavior.FrameMetadata frameMetadata = ImageBehavior.GetFrameMetadata((BitmapDecoder)decoder, gifFile, frameIndex); BitmapSource frame2 = ImageBehavior.MakeFrame(fullSize, (BitmapSource)frame1, frameMetadata, baseFrame); DiscreteObjectKeyFrame discreteObjectKeyFrame = new DiscreteObjectKeyFrame((object)frame2, (KeyTime)zero); animation2.KeyFrames.Add((ObjectKeyFrame)discreteObjectKeyFrame); zero += frameMetadata.Delay; switch (frameMetadata.DisposalMethod) { case ImageBehavior.FrameDisposalMethod.None: case ImageBehavior.FrameDisposalMethod.DoNotDispose: baseFrame = frame2; break; case ImageBehavior.FrameDisposalMethod.RestoreBackground: baseFrame = !ImageBehavior.IsFullFrame(frameMetadata, fullSize) ? ImageBehavior.ClearArea(frame2, frameMetadata) : (BitmapSource)null; break; } ++frameIndex; } animation2.Duration = (Duration)zero; animation2.RepeatBehavior = ImageBehavior.GetActualRepeatBehavior(imageControl, (BitmapDecoder)decoder, gifFile); AnimationCache.AddAnimation((ImageSource)source, ImageBehavior.GetRepeatBehavior(imageControl), animation2); AnimationCache.IncrementReferenceCount((ImageSource)source, ImageBehavior.GetRepeatBehavior(imageControl)); return(animation2); }
private static void RepeatBehaviorChanged( DependencyObject o, DependencyPropertyChangedEventArgs e) { if (!(o is Image imageControl)) { return; } ImageSource animatedSource = ImageBehavior.GetAnimatedSource(imageControl); if (animatedSource == null) { return; } if (!object.Equals(e.OldValue, e.NewValue)) { AnimationCache.DecrementReferenceCount(animatedSource, (RepeatBehavior)e.OldValue); } if (!imageControl.IsLoaded) { return; } ImageBehavior.InitAnimationOrImage(imageControl); }