MakeGif.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Collections.Generic
  8. Imports System.Linq
  9. Imports System.Numerics
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Text
  12. Imports GrapeCity.Documents.Imaging
  13.  
  14. '' This sample loads the image of a goldfish (same as the one used in BmpTransforms)
  15. '' and applies different transformations to it, creating a number of frames that
  16. '' are combined to produce an animated GIF.
  17. Public Class MakeGif
  18. Function GenerateImageStream(
  19. ByVal targetMime As String,
  20. ByVal pixelSize As Size,
  21. ByVal dpi As Single,
  22. ByVal opaque As Boolean,
  23. Optional sampleParams As String() = Nothing) As Stream
  24.  
  25. If Not targetMime = MimeTypes.GIF Then
  26. Throw New Exception("This sample only supports GIF output format.")
  27. End If
  28.  
  29. '' Keep GIF size reasonable:
  30. Dim side2 = Math.Min(400, Math.Min(pixelSize.Width, pixelSize.Height))
  31.  
  32. '' Prepare frames for the target GIF flipping/rotating a single image:
  33. Dim move1() As GcBitmap = New GcBitmap(2) {}
  34. Dim move2() As GcBitmap = New GcBitmap(3) {}
  35. Using bmpSrc = New GcBitmap(Path.Combine("Resources", "Stock", "goldfish.jpg"))
  36. bmpSrc.Opaque = opaque
  37. '' Adjust straight And flipped images to try And keep the fish's head motionless:
  38. Using tbmp = bmpSrc.Resize(side2, side2)
  39. move1(0) = New GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY)
  40. move1(0).Clear(Color.White)
  41. move1(0).BitBlt(tbmp, -CType(side2 / 14.0F, Integer), 0)
  42. End Using
  43. Using tbmp = move1(0).FlipRotate(FlipRotateAction.FlipHorizontal)
  44. move1(1) = New GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY)
  45. move1(1).Clear(Color.White)
  46. move1(1).BitBlt(tbmp, -CType(side2 / 14.0F, Integer), 0)
  47. move1(1).BitBlt(tbmp, 0, 0)
  48. End Using
  49. move2(0) = move1(0).FlipRotate(FlipRotateAction.Rotate90)
  50. move2(1) = move1(0).FlipRotate(FlipRotateAction.Rotate180)
  51. move2(2) = move1(0).FlipRotate(FlipRotateAction.Rotate270)
  52. End Using
  53. '' Combine the moves:
  54. Dim bmps = New List(Of GcBitmap)()
  55. For i = 0 To 3
  56. bmps.Add(move1(0))
  57. bmps.Add(move1(1))
  58. Next
  59. bmps.Add(move1(0))
  60. For i = 0 To 2
  61. bmps.Add(move1(0))
  62. bmps.Add(move2(0))
  63. bmps.Add(move2(1))
  64. bmps.Add(move2(2))
  65. Next
  66. bmps.Add(move1(0))
  67. '' Create the GIF:
  68. Dim ms = New MemoryStream()
  69. Using gw = New GcGifWriter(ms)
  70. gw.LogicalScreenWidth = bmps(0).PixelWidth
  71. gw.LogicalScreenHeight = bmps(0).PixelHeight
  72. gw.PixelAspectRatio = 1
  73. gw.AllowAddingTransparentColor = False
  74.  
  75. For Each bmp In bmps
  76. gw.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 16)
  77. Next
  78. End Using
  79. '' Dispose bitmaps used to create GIF frames:
  80. For Each bmp In bmps.Distinct()
  81. bmp.Dispose()
  82. Next
  83.  
  84. ms.Seek(0, SeekOrigin.Begin)
  85. Return ms
  86. End Function
  87.  
  88. Public ReadOnly Property DefaultMime() As String
  89. Get
  90. Return MimeTypes.GIF
  91. End Get
  92. End Property
  93. End Class
  94.