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); }
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); }
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); }