IndexedGif.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Imaging;
  9. using GrapeCity.Documents.Drawing;
  10. using DsImagingWeb.Demos.Common;
  11.  
  12. namespace DsImagingWeb.Demos
  13. {
  14. // This sample loads an existing GIF with true color frames
  15. // (produced by the MakeGif sample) and converts its frames
  16. // to indexed 8bpp images. It also adds a global palette to the
  17. // resulting GIF (the palette is taken from the first frame).
  18. public class IndexedGif
  19. {
  20. public string DefaultMime { get => Common.Util.MimeTypes.GIF; }
  21.  
  22. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. if (targetMime != Util.MimeTypes.GIF)
  25. throw new Exception("This sample only supports GIF output format.");
  26.  
  27. var ms = new MemoryStream();
  28. // Read frames from the source GIF, convert them to 8bpp and save in the target GIF:
  29. using (var gr = new GcGifReader(Path.Combine("Resources", "Gifs", "goldfish.gif")))
  30. using (var gw = new GcGifWriter(ms))
  31. {
  32. for (int i = 0; i < gr.Frames.Count; ++i)
  33. {
  34. // Add a global palette to the target GIF based on the first frame:
  35. if (i == 0)
  36. using (var tbmp = gr.Frames[i].ToGcBitmap())
  37. gw.GlobalPalette = tbmp.GenerateOctreePalette(256);
  38.  
  39. var indexedBmp = gr.Frames[i].ReadAsIndexed8bppBitmap();
  40. gw.AppendFrame(indexedBmp, 0, 0, GifDisposalMethod.DoNotDispose, 16);
  41. }
  42. }
  43. ms.Seek(0, SeekOrigin.Begin);
  44. return ms;
  45. }
  46. }
  47. }
  48.