private void timer1_Tick(object sender, EventArgs e) { encoder.AddFrame(chart1.GetImage()); timeSelector1.T1 = timeSelector1.T1.AddDays(1).Date; UpdateChart(); if (this.timeSelector1.T1 > timeSelector1.T2) { timer1.Enabled = false; gif.Close(); encoder = null; } }
/// <summary>Saves the images as frames to an animated Gif Image.</summary> /// https://psycodedeveloper.wordpress.com/2013/04/26/saving-an-animated-gif-image-with-c-in-windows-forms/ /// <param name="images">The images to save.</param> /// <param name="path">The path of the Gif file to create.</param> /// <param name="delay">The delay between frames, in milliseconds.</param> /// <param name="repeat">The number of times the animation should repeat. Leave this zero /// for it to loop forever, or specify a value to limit the number of repetitions.</param> public static void SaveAnimatedGifImage(this IEnumerable <Bitmap> images, string path, int delay = 100, int repeat = 0) { var imageArray = images.ToArray(); using (var stream = new MemoryStream()) { using (var encoder = new BumpKit.GifEncoder(stream, null, null, repeat)) { for (int i = 0; i < imageArray.Length; i++) { encoder.AddFrame(imageArray[i].CopyImage(), 0, 0, TimeSpan.FromMilliseconds(delay)); } } stream.Position = 0; using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 0x2000, false)) { stream.WriteTo(fileStream); } } }