From 4554e494fda766ec4f86e95e4f7ef5d42077a02e Mon Sep 17 00:00:00 2001 From: RyanThiele Date: Mon, 8 Jun 2020 13:06:10 -0400 Subject: [PATCH 1/4] Updated `AddFrame` method in `GifEncoder.cs` fix corrupted gif images as a source. --- BumpKit/BumpKit/GifEncoder.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/BumpKit/BumpKit/GifEncoder.cs b/BumpKit/BumpKit/GifEncoder.cs index b4bb0df..2627f0d 100644 --- a/BumpKit/BumpKit/GifEncoder.cs +++ b/BumpKit/BumpKit/GifEncoder.cs @@ -71,6 +71,19 @@ public void AddFrame(Image img, int x = 0, int y = 0, TimeSpan? frameDelay = nul { using (var gifStream = new MemoryStream()) { + // if img is a gif, we need to convert it first. + if (ImageFormat.Gif.Equals(img.RawFormat)) + { + using (MemoryStream ms = new MemoryStream()) + { + // save to memory + img.Save(ms, ImageFormat.Png); + ms.Seek(0, SeekOrigin.Begin); + // pull it back out + img = Image.FromStream(ms); + } + } + img.Save(gifStream, ImageFormat.Gif); if (_isFirstImage) // Steal the global color table info { From df951b71ef18fdb49a2ecde95663309ff4b542cb Mon Sep 17 00:00:00 2001 From: RyanThiele Date: Mon, 8 Jun 2020 13:15:38 -0400 Subject: [PATCH 2/4] Renamed `button1` to `btnOpen` Renamed `button1_Click()` to `btnOpen_Click()` Updated `OpenDialog.Filter` in `Run Demonstrations` button event hander. --- BumpKit/Demonstrations/Form1.Designer.cs | 22 +++++++++++----------- BumpKit/Demonstrations/Form1.cs | 22 +++++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/BumpKit/Demonstrations/Form1.Designer.cs b/BumpKit/Demonstrations/Form1.Designer.cs index 7b8bf71..c0099d4 100644 --- a/BumpKit/Demonstrations/Form1.Designer.cs +++ b/BumpKit/Demonstrations/Form1.Designer.cs @@ -28,7 +28,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.button1 = new System.Windows.Forms.Button(); + this.btnOpen = new System.Windows.Forms.Button(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this._rotateOverflow = new System.Windows.Forms.Panel(); this.label5 = new System.Windows.Forms.Label(); @@ -54,15 +54,15 @@ private void InitializeComponent() ((System.ComponentModel.ISupportInitialize)(this._gifGeneration)).BeginInit(); this.SuspendLayout(); // - // button1 + // btnOpen // - this.button1.Location = new System.Drawing.Point(12, 12); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(792, 23); - this.button1.TabIndex = 0; - this.button1.Text = "Run Demonstrations"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.btnOpen.Location = new System.Drawing.Point(12, 12); + this.btnOpen.Name = "btnOpen"; + this.btnOpen.Size = new System.Drawing.Size(792, 23); + this.btnOpen.TabIndex = 0; + this.btnOpen.Text = "Run Demonstrations"; + this.btnOpen.UseVisualStyleBackColor = true; + this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click); // // groupBox1 // @@ -261,7 +261,7 @@ private void InitializeComponent() this.ClientSize = new System.Drawing.Size(816, 354); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); - this.Controls.Add(this.button1); + this.Controls.Add(this.btnOpen); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; @@ -276,7 +276,7 @@ private void InitializeComponent() #endregion - private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button btnOpen; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Panel _scaleToFit; private System.Windows.Forms.Label label1; diff --git a/BumpKit/Demonstrations/Form1.cs b/BumpKit/Demonstrations/Form1.cs index 206c6a6..db70c3b 100644 --- a/BumpKit/Demonstrations/Form1.cs +++ b/BumpKit/Demonstrations/Form1.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; +using BumpKit; +using System; using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.Linq; -using System.Text; using System.Windows.Forms; -using BumpKit; namespace Demonstrations { @@ -19,9 +14,14 @@ public Demonstrations() InitializeComponent(); } - private void button1_Click(object sender, EventArgs e) + private void btnOpen_Click(object sender, EventArgs e) { - var dlg = new OpenFileDialog { Title = "Please select an image to open:", Filter = "Jpeg|*.jpg|Jpeg|*.jpeg|Png|*.png|Bitmap|*.bmp" }; + var dlg = new OpenFileDialog + { + Title = "Please select an image to open:", + Filter = "All Supported Images|*gif; *jpg; *png; *.bmp|Gif|*.gif|Jpeg|*.jpg|Jpeg|*.jpeg|Png|*.png|Bitmap|*.bmp" + }; + if (dlg.ShowDialog() != DialogResult.OK) return; @@ -38,7 +38,7 @@ private void button1_Click(object sender, EventArgs e) // Rotate and fit _rotateFit.BackgroundImage = img.ScaleToFit(_rotateFit.Size, false).Rotate(45, ScalingMode.FitContent).ScaleToFit(_rotateOverflow.Size); - + // Rotate and overflow _rotateOverflow.BackgroundImage = img.ScaleToFit(_rotateOverflow.Size, false).Rotate(45); @@ -86,7 +86,7 @@ private void button1_Click(object sender, EventArgs e) using (var gfx = Graphics.FromImage(_textGen.BackgroundImage)) { gfx.DrawString("A B C 1 2 3", new Font(FontFamily.Families.First(f => f.Name.Contains("Times")), 15), Brushes.Green, 15, 25, 10, - new[] {Color.Yellow, Color.Blue, Color.Red, Color.Green, Color.Purple, Color.Black}, + new[] { Color.Yellow, Color.Blue, Color.Red, Color.Green, Color.Purple, Color.Black }, new[] { 0, (float).20, (float).40, (float).60, (float).80, 1 }); } } From 0c3f6304d367cd1bf8162975a43c66289421d7df Mon Sep 17 00:00:00 2001 From: RyanThiele Date: Mon, 15 Jun 2020 23:34:06 -0400 Subject: [PATCH 3/4] `img` argument was pass by reference, so it should be not be changed. Instead, a new variable named `imageToSave` was added to the method. Eventhough the method should dispose of the object, a dispose call was also added as well to ensure of disposal. --- BumpKit/BumpKit/GifEncoder.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/BumpKit/BumpKit/GifEncoder.cs b/BumpKit/BumpKit/GifEncoder.cs index 2627f0d..f0c9da9 100644 --- a/BumpKit/BumpKit/GifEncoder.cs +++ b/BumpKit/BumpKit/GifEncoder.cs @@ -69,6 +69,9 @@ public GifEncoder(Stream stream, int? width = null, int? height = null, int? rep /// The positioning y offset this image should be displayed at. public void AddFrame(Image img, int x = 0, int y = 0, TimeSpan? frameDelay = null) { + // assign to a variable used to make the frame. + Image imageToSave = img; + using (var gifStream = new MemoryStream()) { // if img is a gif, we need to convert it first. @@ -77,20 +80,24 @@ public void AddFrame(Image img, int x = 0, int y = 0, TimeSpan? frameDelay = nul using (MemoryStream ms = new MemoryStream()) { // save to memory - img.Save(ms, ImageFormat.Png); + imageToSave.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); // pull it back out - img = Image.FromStream(ms); + imageToSave = Image.FromStream(ms); } } - img.Save(gifStream, ImageFormat.Gif); + // save the image to the frame. + imageToSave.Save(gifStream, ImageFormat.Gif); if (_isFirstImage) // Steal the global color table info { InitHeader(gifStream, img.Width, img.Height); } WriteGraphicControlBlock(gifStream, frameDelay.GetValueOrDefault(FrameDelay)); WriteImageBlock(gifStream, !_isFirstImage, x, y, img.Width, img.Height); + + // dispose of the image saved to frame. + if (imageToSave != null) imageToSave.Dispose(); } _isFirstImage = false; } From e75aa0b440f898a726c14d0c7b1ea56650fdd732 Mon Sep 17 00:00:00 2001 From: RyanThiele Date: Tue, 16 Jun 2020 02:01:48 -0400 Subject: [PATCH 4/4] Updated method for better memory management. --- .gitignore | 1 + BumpKit/BumpKit/GifEncoder.cs | 35 +++++++++++---------------- BumpKit/Demonstrations/Program.cs | 39 ++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 5ebd21a..2e5ad11 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,4 @@ pip-log.txt # Mac crap .DS_Store +/.vs diff --git a/BumpKit/BumpKit/GifEncoder.cs b/BumpKit/BumpKit/GifEncoder.cs index f0c9da9..32fa9d2 100644 --- a/BumpKit/BumpKit/GifEncoder.cs +++ b/BumpKit/BumpKit/GifEncoder.cs @@ -69,37 +69,30 @@ public GifEncoder(Stream stream, int? width = null, int? height = null, int? rep /// The positioning y offset this image should be displayed at. public void AddFrame(Image img, int x = 0, int y = 0, TimeSpan? frameDelay = null) { - // assign to a variable used to make the frame. - Image imageToSave = img; + Image imageToSave = null; - using (var gifStream = new MemoryStream()) + using (MemoryStream ms = new MemoryStream()) { - // if img is a gif, we need to convert it first. - if (ImageFormat.Gif.Equals(img.RawFormat)) - { - using (MemoryStream ms = new MemoryStream()) - { - // save to memory - imageToSave.Save(ms, ImageFormat.Png); - ms.Seek(0, SeekOrigin.Begin); - // pull it back out - imageToSave = Image.FromStream(ms); - } - } + // save to memory + img.Save(ms, ImageFormat.Png); + ms.Seek(0, SeekOrigin.Begin); + // pull it back out + imageToSave = Image.FromStream(ms); + } - // save the image to the frame. + using (var gifStream = new MemoryStream()) + { imageToSave.Save(gifStream, ImageFormat.Gif); if (_isFirstImage) // Steal the global color table info { - InitHeader(gifStream, img.Width, img.Height); + InitHeader(gifStream, imageToSave.Width, imageToSave.Height); } WriteGraphicControlBlock(gifStream, frameDelay.GetValueOrDefault(FrameDelay)); - WriteImageBlock(gifStream, !_isFirstImage, x, y, img.Width, img.Height); - - // dispose of the image saved to frame. - if (imageToSave != null) imageToSave.Dispose(); + WriteImageBlock(gifStream, !_isFirstImage, x, y, imageToSave.Width, imageToSave.Height); } _isFirstImage = false; + + if (imageToSave != null) imageToSave.Dispose(); } private void InitHeader(Stream sourceGif, int w, int h) diff --git a/BumpKit/Demonstrations/Program.cs b/BumpKit/Demonstrations/Program.cs index ac64e33..507aa41 100644 --- a/BumpKit/Demonstrations/Program.cs +++ b/BumpKit/Demonstrations/Program.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using BumpKit; +using System; using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Text; -using System.Linq; -using System.Windows.Forms; -using BumpKit; +using System.IO; namespace Demonstrations { @@ -18,9 +13,31 @@ static class Program [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Demonstrations()); + //Application.EnableVisualStyles(); + //Application.SetCompatibleTextRenderingDefault(false); + //Application.Run(new Demonstrations()); + + + using (FileStream fs = new FileStream(@"E:\123.gif", FileMode.OpenOrCreate)) + { + GifEncoder gifEncoder = new GifEncoder(fs); + + foreach (string item in Directory.EnumerateFiles(@"E:\Pictures")) + { + try + { + Image img = Image.FromFile(item); + gifEncoder.AddFrame(Image.FromFile(item)); + } + catch + { + + } + + } + } + + } } }