private static BitmapSource MakeFrame( ImageBehavior.Int32Size fullSize, BitmapSource rawFrame, ImageBehavior.FrameMetadata metadata, BitmapSource baseFrame) { if (baseFrame == null && ImageBehavior.IsFullFrame(metadata, fullSize)) { return(rawFrame); } DrawingVisual drawingVisual = new DrawingVisual(); using (DrawingContext drawingContext = drawingVisual.RenderOpen()) { if (baseFrame != null) { Rect rectangle = new Rect(0.0, 0.0, (double)fullSize.Width, (double)fullSize.Height); drawingContext.DrawImage((ImageSource)baseFrame, rectangle); } Rect rectangle1 = new Rect((double)metadata.Left, (double)metadata.Top, (double)metadata.Width, (double)metadata.Height); drawingContext.DrawImage((ImageSource)rawFrame, rectangle1); } RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(fullSize.Width, fullSize.Height, 96.0, 96.0, PixelFormats.Pbgra32); renderTargetBitmap.Render((Visual)drawingVisual); if (renderTargetBitmap.CanFreeze && !renderTargetBitmap.IsFrozen) { renderTargetBitmap.Freeze(); } return((BitmapSource)renderTargetBitmap); }
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); }