MakeGif.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 System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13.  
  14. namespace DsImagingWeb.Demos
  15. {
  16. // This sample loads the image of a goldfish (same as the one used in BmpTransforms)
  17. // and applies different transformations to it, creating a number of frames that
  18. // are combined to produce an animated GIF.
  19. public class MakeGif
  20. {
  21. public string DefaultMime { get => Common.Util.MimeTypes.GIF; }
  22.  
  23. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. if (targetMime != Common.Util.MimeTypes.GIF)
  26. throw new Exception("This sample only supports GIF output format.");
  27.  
  28. // Keep GIF size reasonable:
  29. var side2 = Math.Min(400, Math.Min(pixelSize.Width, pixelSize.Height));
  30.  
  31. // Prepare frames for the target GIF flipping/rotating a single image:
  32. var move1 = new GcBitmap[2];
  33. var move2 = new GcBitmap[3];
  34. using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Stock", "goldfish.jpg")))
  35. {
  36. bmpSrc.Opaque = opaque;
  37. // Adjust straight and flipped images to try and keep the fish's head motionless:
  38. using (var tbmp = bmpSrc.Resize(side2, side2))
  39. {
  40. move1[0] = new GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY);
  41. move1[0].Clear(Color.White);
  42. move1[0].BitBlt(tbmp, -(int)(side2 / 14f), 0);
  43. }
  44. using (var tbmp = move1[0].FlipRotate(FlipRotateAction.FlipHorizontal))
  45. {
  46. move1[1] = new GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY);
  47. move1[1].Clear(Color.White);
  48. move1[1].BitBlt(tbmp, (int)(side2 / 14f), 0);
  49. move1[1].BitBlt(tbmp, 0, 0);
  50. }
  51. move2[0] = move1[0].FlipRotate(FlipRotateAction.Rotate90);
  52. move2[1] = move1[0].FlipRotate(FlipRotateAction.Rotate180);
  53. move2[2] = move1[0].FlipRotate(FlipRotateAction.Rotate270);
  54. }
  55. // Combine the moves:
  56. var bmps = new List<GcBitmap>();
  57. for (int i = 0; i < 4; ++i)
  58. {
  59. bmps.Add(move1[0]);
  60. bmps.Add(move1[1]);
  61. }
  62. bmps.Add(move1[0]);
  63. for (int i = 0; i < 2; ++i)
  64. {
  65. bmps.Add(move1[0]);
  66. bmps.Add(move2[0]);
  67. bmps.Add(move2[1]);
  68. bmps.Add(move2[2]);
  69. }
  70. bmps.Add(move1[0]);
  71. // Create the GIF:
  72. var ms = new MemoryStream();
  73. using (var gw = new GcGifWriter(ms))
  74. {
  75. gw.LogicalScreenWidth = bmps[0].PixelWidth;
  76. gw.LogicalScreenHeight = bmps[0].PixelHeight;
  77. gw.PixelAspectRatio = 1;
  78. gw.AllowAddingTransparentColor = false;
  79.  
  80. foreach (var bmp in bmps)
  81. gw.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 16);
  82. }
  83. // Dispose bitmaps used to create GIF frames:
  84. foreach (var bmp in bmps.Distinct())
  85. bmp.Dispose();
  86.  
  87. ms.Seek(0, SeekOrigin.Begin);
  88. return ms;
  89. }
  90. }
  91. }
  92.