// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Windows.Media.Imaging; // Ref PresentationCore WindowsBase namespace Spludlow.Drawing { public class GIF { private static byte[] _ApplicationExtension = new byte[] { 33, 255, 11, 78, 69, 84, 83, 67, 65, 80, 69, 50, 46, 48, 3, 1, 0, 0, 0 }; public static void MakeGif(MemoryStream[] frameMemoryStreams, Stream outputStream) { using (MemoryStream intemediateStream = new MemoryStream()) { GifBitmapEncoder encoder = new GifBitmapEncoder(); foreach (MemoryStream memoryStream in frameMemoryStreams) encoder.Frames.Add(BitmapFrame.Create(memoryStream)); encoder.Save(intemediateStream); intemediateStream.Position = 0; // // Fix the GIF to allow looping // // Copy first 13 bytes byte[] headerBuffer = new byte[13]; intemediateStream.Read(headerBuffer, 0, headerBuffer.Length); outputStream.Write(headerBuffer, 0, headerBuffer.Length); // Inject the extra bit outputStream.Write(_ApplicationExtension, 0, _ApplicationExtension.Length); // Copy remaining intemediateStream.CopyTo(outputStream); } } } }