public GifFrame(GifInfoCache info, ImageStorage image, int frameIndex) { BitmapImage imageSource = new BitmapImage(); Stream bitmapStream = new MemoryStream(); image.Save(bitmapStream, ImageFormat.Png); bitmapStream.Seek(0, SeekOrigin.Begin); imageSource.BeginInit(); imageSource.StreamSource = bitmapStream; imageSource.EndInit(); if (imageSource.CanFreeze) { imageSource.Freeze(); } this.ImageSource = imageSource; this.DelayToNextMS = BitConverter.ToInt32(image.GetPropertyItem(kFrameDelayProperty).Value, frameIndex) * 10; if (this.DelayToNextMS == 0 && info.Frames.Count > 0) { this.DelayToNextMS = info.Frames[0].DelayToNextMS; } else { this.DelayToNextMS = 100; } this.FrameIndex = frameIndex; }
private static void HandleCurrentFrameIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs <int> e) { GifInfoCache info = GetInfo(d); if (info == null) { return; } if (e.NewValue >= info.Frames.Count) { if (IsLooping(d, info)) { SetCurrentFrameIndex(d, 0); return; } else { SetCurrentFrameIndex(d, info.Frames.Count - 1); SetIsPlaying(d, false); return; } } if (e.NewValue >= 0 && d is ImageControl imageControl) { imageControl.Source = info.Frames[e.NewValue].ImageSource; } }
internal static void SetInfo(DependencyObject obj, GifInfoCache value) => obj.SetValue(InfoPropertyKey, value);
// ==================[ Utilities ]======================== private static bool IsLooping(DependencyObject d, GifInfoCache info) => d.IsSetLocally(OverrideIsLoopingProperty) ? GetOverrideIsLooping(d) : info.IsLooping;