IndexedGif.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 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. Function GenerateImageStream(
  20. ByVal targetMime As String,
  21. ByVal pixelSize As Size,
  22. ByVal dpi As Single,
  23. ByVal opaque As Boolean,
  24. Optional sampleParams As String() = Nothing) As Stream
  25.  
  26. If Not targetMime = MimeTypes.GIF Then
  27. Throw New Exception("This sample only supports GIF output format.")
  28. End If
  29.  
  30. Dim ms = New MemoryStream()
  31. '' Read frames from the source GIF, convert them to 8bpp And save in the target GIF
  32. Using gr = New GcGifReader(Path.Combine("Resources", "Gifs", "goldfish.gif")), gw = New GcGifWriter(ms)
  33. For i = 0 To gr.Frames.Count - 1
  34. '' Add a global palette to the target GIF based on the first frame
  35. If i = 0 Then
  36. Using tbmp = gr.Frames(i).ToGcBitmap()
  37. gw.GlobalPalette = tbmp.GenerateOctreePalette(256)
  38. End Using
  39. End If
  40. Dim indexedBmp = gr.Frames(i).ReadAsIndexed8bppBitmap()
  41. gw.AppendFrame(indexedBmp, 0, 0, GifDisposalMethod.DoNotDispose, 16)
  42. Next
  43. End Using
  44. ms.Seek(0, SeekOrigin.Begin)
  45. Return ms
  46. End Function
  47.  
  48. Public ReadOnly Property DefaultMime() As String
  49. Get
  50. Return MimeTypes.GIF
  51. End Get
  52. End Property
  53. End Class
  54.