コード例 #1
0
        private static void 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 (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)
                {
                    InitAnimationOrImage(imageControl);
                }
            }
        }
コード例 #2
0
        private static void AnimatedSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Image image = o as Image;

            if (image != null)
            {
                ImageSource oldValue = e.OldValue as ImageSource;
                ImageSource newValue = e.NewValue as ImageSource;
                if (oldValue != null)
                {
                    image.Loaded   -= new RoutedEventHandler(ImageBehavior.ImageControlLoaded);
                    image.Unloaded -= new RoutedEventHandler(ImageBehavior.ImageControlUnloaded);
                    AnimationCache.DecrementReferenceCount(oldValue, GetRepeatBehavior(image));
                    ImageAnimationController animationController = GetAnimationController(image);
                    if (animationController != null)
                    {
                        animationController.Dispose();
                    }
                    image.Source = null;
                }
                if (newValue != null)
                {
                    image.Loaded   += new RoutedEventHandler(ImageBehavior.ImageControlLoaded);
                    image.Unloaded += new RoutedEventHandler(ImageBehavior.ImageControlUnloaded);
                    if (image.IsLoaded)
                    {
                        InitAnimationOrImage(image);
                    }
                }
            }
        }
コード例 #3
0
        private static void 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))
            {
                if (imageControl.IsLoaded)
                {
                    var isAnimLoaded = GetIsAnimationLoaded(imageControl);
                    if (!isAnimLoaded)
                    {
                        InitAnimationOrImage(imageControl);
                    }
                }
                return;
            }
            if (oldValue != null)
            {
                imageControl.Loaded           -= ImageControlLoaded;
                imageControl.Unloaded         -= ImageControlUnloaded;
                imageControl.IsVisibleChanged -= VisibilityChanged;

                AnimationCache.RemoveControlForSource(oldValue, imageControl);
                var controller = GetAnimationController(imageControl);
                if (controller != null)
                {
                    controller.Dispose();
                }
                imageControl.Source = null;
            }
            if (newValue != null)
            {
                imageControl.Loaded           += ImageControlLoaded;
                imageControl.Unloaded         += ImageControlUnloaded;
                imageControl.IsVisibleChanged += VisibilityChanged;

                if (imageControl.IsLoaded)
                {
                    InitAnimationOrImage(imageControl);
                }
            }
        }
コード例 #4
0
        private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source)
        {
            ObjectAnimationUsingKeyFrames animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl));

            if (animation == null)
            {
                GifFile          file;
                GifBitmapDecoder decoder = GetDecoder(source, out file) as GifBitmapDecoder;
                if ((decoder == null) || (decoder.Frames.Count <= 1))
                {
                    return(null);
                }
                Int32Size fullSize   = GetFullSize(decoder, file);
                int       frameIndex = 0;
                animation = new ObjectAnimationUsingKeyFrames();
                TimeSpan     zero      = TimeSpan.Zero;
                BitmapSource baseFrame = null;
                foreach (BitmapFrame frame in decoder.Frames)
                {
                    FrameMetadata          metadata = GetFrameMetadata(decoder, file, frameIndex);
                    BitmapSource           source3  = MakeFrame(fullSize, frame, metadata, baseFrame);
                    DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(source3, zero);
                    animation.KeyFrames.Add(keyFrame);
                    zero += metadata.Delay;
                    FrameDisposalMethod disposalMethod = metadata.DisposalMethod;
                    switch (disposalMethod)
                    {
                    case FrameDisposalMethod.None:
                    case FrameDisposalMethod.DoNotDispose:
                        baseFrame = source3;
                        break;

                    case FrameDisposalMethod.RestoreBackground:
                        baseFrame = !IsFullFrame(metadata, fullSize) ? ClearArea(source3, metadata) : null;
                        break;

                    default:
                        break;
                    }
                    frameIndex++;
                }
                animation.Duration       = zero;
                animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, file);
                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl));
            }
            return(animation);
        }
コード例 #5
0
        private static void ImageControlUnloaded(object sender, RoutedEventArgs e)
        {
            Image image = sender as Image;

            if (image != null)
            {
                ImageSource animatedSource = GetAnimatedSource(image);
                if (animatedSource != null)
                {
                    AnimationCache.DecrementReferenceCount(animatedSource, GetRepeatBehavior(image));
                }
                ImageAnimationController animationController = GetAnimationController(image);
                if (animationController != null)
                {
                    animationController.Dispose();
                }
            }
        }
コード例 #6
0
        private static void RepeatBehaviorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Image image = o as Image;

            if (image != null)
            {
                ImageSource animatedSource = GetAnimatedSource(image);
                if (animatedSource != null)
                {
                    if (!Equals(e.OldValue, e.NewValue))
                    {
                        AnimationCache.DecrementReferenceCount(animatedSource, (RepeatBehavior)e.OldValue);
                    }
                    if (image.IsLoaded)
                    {
                        InitAnimationOrImage(image);
                    }
                }
            }
        }
コード例 #7
0
        static void ImageControlUnloaded(object sender, RoutedEventArgs e)
        {
            Image imageControl = sender as Image;

            if (imageControl == null)
            {
                return;
            }
            var source = GetAnimatedSource(imageControl);

            if (source != null)
            {
                AnimationCache.RemoveControlForSource(source, imageControl);
            }
            var controller = GetAnimationController(imageControl);

            if (controller != null)
            {
                controller.Dispose();
            }
        }
コード例 #8
0
        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();
            }
        }
コード例 #9
0
        private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source)
        {
            var animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl));

            if (animation != null)
            {
                return(animation);
            }
            GifFile gifMetadata;

            var decoder = GetDecoder(source, imageControl, out gifMetadata) as GifBitmapDecoder;

            if (decoder != null && decoder.Frames.Count > 1)
            {
                var animationFrames = CreateGifAnimation(decoder, gifMetadata);
                animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata);

                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl));
                return(animationFrames);
            }
            return(null);
        }
コード例 #10
0
        private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source)
        {
            var cacheEntry = AnimationCache.Get(source);

            if (cacheEntry == null)
            {
                var decoder = GetDecoder(source, imageControl, out GifFile gifMetadata) as GifBitmapDecoder;
                if (decoder != null && decoder.Frames.Count > 1)
                {
                    var          fullSize      = GetFullSize(decoder, gifMetadata);
                    int          index         = 0;
                    var          keyFrames     = new ObjectKeyFrameCollection();
                    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);
                        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++;
                    }

                    int repeatCount = GetRepeatCountFromMetadata(decoder, gifMetadata);
                    cacheEntry = new AnimationCacheEntry(keyFrames, totalDuration, repeatCount);
                    AnimationCache.Add(source, cacheEntry);
                }
            }

            if (cacheEntry != null)
            {
                var animation = new ObjectAnimationUsingKeyFrames
                {
                    KeyFrames      = cacheEntry.KeyFrames,
                    Duration       = cacheEntry.Duration,
                    RepeatBehavior = GetActualRepeatBehavior(imageControl, cacheEntry.RepeatCountFromMetadata),
                    SpeedRatio     = GetActualSpeedRatio(imageControl, cacheEntry.Duration)
                };

                AnimationCache.AddControlForSource(source, imageControl);
                return(animation);
            }

            return(null);
        }
コード例 #11
0
        private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source)
        {
            var 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)
            {
                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);

                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl));
                return(animation);
            }
            return(null);
        }