Beispiel #1
0
        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);
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        private static RepeatBehavior GetActualRepeatBehavior(
            Image imageControl,
            BitmapDecoder decoder,
            GifFile gifMetadata)
        {
            RepeatBehavior repeatBehavior = ImageBehavior.GetRepeatBehavior(imageControl);

            if (repeatBehavior != new RepeatBehavior())
            {
                return(repeatBehavior);
            }
            int num = gifMetadata == null?ImageBehavior.GetRepeatCount(decoder) : (int)gifMetadata.RepeatCount;

            return(num == 0 ? RepeatBehavior.Forever : new RepeatBehavior((double)num));
        }
Beispiel #4
0
        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);
        }