From 846b61beab343b41467dccdce8bd208d7e97b143 Mon Sep 17 00:00:00 2001 From: James South Date: Sun, 2 Nov 2014 14:49:19 +0000 Subject: [PATCH] GifEncoder no longer requires disposal Former-commit-id: 1444d26df14a5c20adb57455e5dc1b183e01fc8c Former-commit-id: be310b1f59ab4d6f1bd282ab2130abbbc8a8cd5a --- .../Imaging/Formats/GifEncoder.cs | 75 +------------------ .../Imaging/Formats/GifFormat.cs | 16 ++-- 2 files changed, 9 insertions(+), 82 deletions(-) diff --git a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs index 9f107aea8..249d5d12c 100644 --- a/src/ImageProcessor/Imaging/Formats/GifEncoder.cs +++ b/src/ImageProcessor/Imaging/Formats/GifEncoder.cs @@ -25,13 +25,11 @@ namespace ImageProcessor.Imaging.Formats /// /// Encodes multiple images as an animated gif to a stream. /// - /// Always wire this up in a using block. - /// Disposing the encoder will complete the file. /// Uses default .NET GIF encoding and adds animation headers. /// Adapted from /// /// - public class GifEncoder : IDisposable + public class GifEncoder { #region Constants /// @@ -127,19 +125,6 @@ namespace ImageProcessor.Imaging.Formats /// private int? height; - /// - /// A value indicating whether this instance of the given entity has been disposed. - /// - /// if this instance has been disposed; otherwise, . - /// - /// If the entity is disposed, it must not be disposed a second - /// time. The isDisposed field is set the first time the entity - /// is disposed. If the isDisposed field is true, then the Dispose() - /// method will not dispose again. This help not to prolong the entity's - /// life in the Garbage Collector. - /// - private bool isDisposed; - /// /// The is first image. /// @@ -176,24 +161,6 @@ namespace ImageProcessor.Imaging.Formats this.height = height; this.repeatCount = repeatCount; } - - /// - /// Finalizes an instance of the class. - /// - /// - /// Use C# destructor syntax for finalization code. - /// This destructor will run only if the Dispose method - /// does not get called. - /// It gives your base class the opportunity to finalize. - /// Do not provide destructors in types derived from this class. - /// - ~GifEncoder() - { - // Do not re-create Dispose clean-up code here. - // Calling Dispose(false) is optimal in terms of - // readability and maintainability. - this.Dispose(false); - } #endregion #region Public Methods and Operators @@ -221,21 +188,6 @@ namespace ImageProcessor.Imaging.Formats this.isFirstImage = false; } - /// - /// Disposes the object and frees resources for the Garbage Collector. - /// - public void Dispose() - { - this.Dispose(true); - - // This object will be cleaned up by the Dispose method. - // Therefore, you should call GC.SupressFinalize to - // take this object off the finalization queue - // and prevent finalization code for this object - // from executing a second time. - GC.SuppressFinalize(this); - } - /// /// Saves the completed gif to an /// @@ -249,35 +201,12 @@ namespace ImageProcessor.Imaging.Formats this.imageStream.Flush(); this.imageStream.Position = 0; byte[] bytes = this.imageStream.ToArray(); + this.imageStream.Dispose(); return (Image)Converter.ConvertFrom(bytes); } #endregion #region Methods - /// - /// Disposes the object and frees resources for the Garbage Collector. - /// - /// - /// If true, the object gets disposed. - /// - protected virtual void Dispose(bool disposing) - { - if (this.isDisposed) - { - return; - } - - if (disposing) - { - this.imageStream.Dispose(); - } - - // Call the appropriate methods to clean up - // unmanaged resources here. - // Note disposing is done. - this.isDisposed = true; - } - /// /// Writes the header block of the animated gif to the stream. /// diff --git a/src/ImageProcessor/Imaging/Formats/GifFormat.cs b/src/ImageProcessor/Imaging/Formats/GifFormat.cs index 091db1037..c36457ae5 100644 --- a/src/ImageProcessor/Imaging/Formats/GifFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/GifFormat.cs @@ -79,17 +79,15 @@ namespace ImageProcessor.Imaging.Formats if (decoder.IsAnimated) { OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); - using (GifEncoder encoder = new GifEncoder(null, null, decoder.LoopCount)) + GifEncoder encoder = new GifEncoder(null, null, decoder.LoopCount); + foreach (GifFrame frame in decoder.GifFrames) { - foreach (GifFrame frame in decoder.GifFrames) - { - factory.Image = frame.Image; - frame.Image = quantizer.Quantize(processor.Invoke(factory)); - encoder.AddFrame(frame); - } - - factory.Image = encoder.Save(); + factory.Image = frame.Image; + frame.Image = quantizer.Quantize(processor.Invoke(factory)); + encoder.AddFrame(frame); } + + factory.Image = encoder.Save(); } else {