コード例 #1
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);
        }