/// <param name="filename">Animated Image (ie: animated gif).</param> public static AnimatedImageData GetAnimatedImageData(string fileName) { lock (AnimatedImageDatas) { int index = AnimatedImageDataFileNames.IndexOf(fileName); if (index >= 0) { // AnimatedImageData Already Exists // move it to the end of the array and return it AnimatedImageData data = AnimatedImageDatas[index]; string name = AnimatedImageDataFileNames[index]; AnimatedImageDatas.RemoveAt(index); AnimatedImageDataFileNames.RemoveAt(index); AnimatedImageDatas.Add(data); AnimatedImageDataFileNames.Add(name); return(AnimatedImageDatas[AnimatedImageDatas.Count - 1]); } else { // New AnimatedImageData System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); AnimatedImageData data = new AnimatedImageData(); //// Get Frame Duration int frameDuration = 0; try { System.Drawing.Imaging.PropertyItem frameDelay = image.GetPropertyItem(0x5100); frameDuration = (frameDelay.Value[0] + frameDelay.Value[1] * 256) * 10; } catch { } if (frameDuration > 10) { data.FrameDuration = frameDuration; } else { data.FrameDuration = AnimatedImage.DEFAULT_FRAME_DURATION; } //// Store AnimatedImageData AnimatedImageDatas.Add(data); AnimatedImageDataFileNames.Add(fileName); // Limit amount of Animations in Memory if (AnimatedImageDatas.Count > MAX_ANIMATIONS) { AnimatedImageDatas[0].CancelLoading = true; for (int i = 0; i < AnimatedImageDatas[0].Frames.Count; i++) { AnimatedImageDatas[0]?.Frames[i]?.Dispose(); } AnimatedImageDatas.RemoveAt(0); AnimatedImageDataFileNames.RemoveAt(0); } //// Get Frames LoadingAnimatedImage loadingAnimatedImage = new LoadingAnimatedImage(image, data); Thread loadFramesThread = new Thread(new ThreadStart(loadingAnimatedImage.LoadFrames)); loadFramesThread.Name = "AnimationLoadThread - " + fileName; loadFramesThread.IsBackground = true; loadFramesThread.Start(); while (data.Frames.Count <= 0) { ; // wait for at least one frame to be loaded } return(data); } } }
/// <param name="filename">Animated Image (ie: animated gif).</param> public static AnimatedImageData GetAnimatedImageData(string fileName) { int index = AnimatedImageDataFileNames.IndexOf(fileName); if (index >= 0) { // AnimatedImageData Already Exists // move it to the end of the array and return it AnimatedImageData data = AnimatedImageDatas[index]; string name = AnimatedImageDataFileNames[index]; AnimatedImageDatas.RemoveAt(index); AnimatedImageDataFileNames.RemoveAt(index); AnimatedImageDatas.Add(data); AnimatedImageDataFileNames.Add(name); return AnimatedImageDatas[AnimatedImageDatas.Count-1]; } else { // New AnimatedImageData System.Drawing.Image image = System.Drawing.Image.FromFile(fileName); AnimatedImageData data = new AnimatedImageData(); //// Get Frame Duration System.Drawing.Imaging.PropertyItem frameDelay = image.GetPropertyItem(0x5100); int frameDuration = (frameDelay.Value[0] + frameDelay.Value[1] * 256) * 10; if (frameDuration > 10) data.FrameDuration = frameDuration; else data.FrameDuration = AnimatedImage.DEFAULT_FRAME_DURATION; //// Store AnimatedImageData AnimatedImageDatas.Add(data); AnimatedImageDataFileNames.Add(fileName); // Limit amount of Animations in Memory if (AnimatedImageDatas.Count >= MAX_ANIMATIONS) { for (int i = 0; i < AnimatedImageDatas[0].Frames.Count; i++) AnimatedImageDatas[0].Frames[i].Dispose(); AnimatedImageDatas.RemoveAt(0); AnimatedImageDataFileNames.RemoveAt(0); } //// Get Frames LoadingAnimatedImage loadingAnimatedImage = new LoadingAnimatedImage(image, data); Thread loadFramesThread = new Thread(new ThreadStart(loadingAnimatedImage.LoadFrames)); loadFramesThread.IsBackground = true; loadFramesThread.Start(); while (data.Frames.Count <= 0); // wait for at least one frame to be loaded return data; } }