private static ImageBehavior.FrameMetadata GetFrameMetadata( BitmapDecoder decoder, GifFile gifMetadata, int frameIndex) { return(gifMetadata != null && gifMetadata.Frames.Count > frameIndex?ImageBehavior.GetFrameMetadata(gifMetadata.Frames[frameIndex]) : ImageBehavior.GetFrameMetadata(decoder.Frames[frameIndex])); }
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); }
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 void ImageControlLoaded(object sender, RoutedEventArgs e) { if (!(sender is Image imageControl)) { return; } ImageBehavior.InitAnimationOrImage(imageControl); }
private static int GetRepeatCount(BitmapDecoder decoder) { BitmapMetadata applicationExtension = ImageBehavior.GetApplicationExtension(decoder, "NETSCAPE2.0"); if (applicationExtension != null) { byte[] queryOrNull = applicationExtension.GetQueryOrNull <byte[]>("/Data"); if (queryOrNull != null && queryOrNull.Length >= 4) { return((int)BitConverter.ToUInt16(queryOrNull, 2)); } } return(1); }
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(); }
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)); }
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); }
private static void InitAnimationOrImage(Image imageControl) { ImageBehavior.SetAnimationController((DependencyObject)imageControl, (ImageAnimationController)null); ImageBehavior.SetIsAnimationLoaded(imageControl, false); BitmapSource source = ImageBehavior.GetAnimatedSource(imageControl) as BitmapSource; bool flag1 = (DesignerProperties.GetIsInDesignMode((DependencyObject)imageControl) ? 1 : 0) == 0 | ImageBehavior.GetAnimateInDesignMode((DependencyObject)imageControl); bool flag2 = ImageBehavior.IsLoadingDeferred(source); if (source != null & flag1 && !flag2) { if (source.IsDownloading) { EventHandler handler = (EventHandler)null; handler = (EventHandler)((sender, args) => { source.DownloadCompleted -= handler; ImageBehavior.InitAnimationOrImage(imageControl); }); source.DownloadCompleted += handler; imageControl.Source = (ImageSource)source; return; } ObjectAnimationUsingKeyFrames animation = ImageBehavior.GetAnimation(imageControl, source); if (animation != null) { if (animation.KeyFrames.Count > 0) { ImageBehavior.TryTwice((System.Action)(() => imageControl.Source = (ImageSource)animation.KeyFrames[0].Value)); } else { imageControl.Source = (ImageSource)source; } ImageAnimationController animationController = new ImageAnimationController(imageControl, animation, ImageBehavior.GetAutoStart(imageControl)); ImageBehavior.SetAnimationController((DependencyObject)imageControl, animationController); ImageBehavior.SetIsAnimationLoaded(imageControl, true); imageControl.RaiseEvent(new RoutedEventArgs(ImageBehavior.AnimationLoadedEvent, (object)imageControl)); return; } } imageControl.Source = (ImageSource)source; if (source == null) { return; } ImageBehavior.SetIsAnimationLoaded(imageControl, true); imageControl.RaiseEvent(new RoutedEventArgs(ImageBehavior.AnimationLoadedEvent, (object)imageControl)); }
private static void AnimateInDesignModeChanged( DependencyObject o, DependencyPropertyChangedEventArgs e) { if (!(o is Image imageControl)) { return; } bool newValue = (bool)e.NewValue; if (ImageBehavior.GetAnimatedSource(imageControl) == null || !imageControl.IsLoaded) { return; } if (newValue) { ImageBehavior.InitAnimationOrImage(imageControl); } else { imageControl.BeginAnimation(Image.SourceProperty, (AnimationTimeline)null); } }
private static void RepeatBehaviorChanged( DependencyObject o, DependencyPropertyChangedEventArgs e) { if (!(o is Image imageControl)) { return; } ImageSource animatedSource = ImageBehavior.GetAnimatedSource(imageControl); if (animatedSource == null) { return; } if (!object.Equals(e.OldValue, e.NewValue)) { AnimationCache.DecrementReferenceCount(animatedSource, (RepeatBehavior)e.OldValue); } if (!imageControl.IsLoaded) { return; } ImageBehavior.InitAnimationOrImage(imageControl); }
private static BitmapDecoder GetDecoder(BitmapSource image, out GifFile gifFile) { gifFile = (GifFile)null; BitmapDecoder decoder = (BitmapDecoder)null; Stream stream = (Stream)null; Uri result = (Uri)null; BitmapCreateOptions createOptions = BitmapCreateOptions.None; if (image is BitmapImage bitmapImage) { createOptions = bitmapImage.CreateOptions; if (bitmapImage.StreamSource != null) { stream = bitmapImage.StreamSource; } else if (bitmapImage.UriSource != (Uri)null) { result = bitmapImage.UriSource; if (bitmapImage.BaseUri != (Uri)null && !result.IsAbsoluteUri) { result = new Uri(bitmapImage.BaseUri, result); } } } else if (image is BitmapFrame bitmapFrame) { decoder = bitmapFrame.Decoder; Uri.TryCreate(bitmapFrame.BaseUri, bitmapFrame.ToString((IFormatProvider)CultureInfo.InvariantCulture), out result); } if (decoder == null) { if (stream != null) { stream.Position = 0L; decoder = BitmapDecoder.Create(stream, createOptions, BitmapCacheOption.OnLoad); } else if (result != (Uri)null && result.IsAbsoluteUri) { decoder = BitmapDecoder.Create(result, createOptions, BitmapCacheOption.OnLoad); } } if (decoder is GifBitmapDecoder && !ImageBehavior.CanReadNativeMetadata(decoder)) { if (stream != null) { stream.Position = 0L; gifFile = GifFile.ReadGifFile(stream, true); } else { if (!(result != (Uri)null)) { throw new InvalidOperationException("Can't get URI or Stream from the source. AnimatedSource should be either a BitmapImage, or a BitmapFrame constructed from a URI."); } gifFile = ImageBehavior.DecodeGifFile(result); } } if (decoder == null) { throw new InvalidOperationException("Can't get a decoder from the source. AnimatedSource should be either a BitmapImage or a BitmapFrame."); } return(decoder); }