UpdateGif.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 and modifies its frames.
  15. '' Specifically, it applies a color matrix to change the colors,
  16. '' and flips the images horizontally.
  17. '' The GIF loaded in this sample is the one produced by IndexedGif.
  18. Public Class UpdateGif
  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. '' Matrix to change colors to greenish blues:
  31. Dim colorMatrix = New ColorMatrix5x4() With
  32. {
  33. .M11 = 0.2F,
  34. .M12 = 0.3F,
  35. .M22 = 0.5F
  36. }
  37.  
  38. Dim ms = New MemoryStream()
  39. '' Read source GIF, write modified one (change palette And flip And rotate frames)
  40. Using gr = New GcGifReader(Path.Combine("Resources", "Gifs", "goldfish-indexed.gif")), gw = New GcGifWriter(ms)
  41.  
  42. Dim pal = gr.GetGlobalPalette()
  43. '' This sample will only work with GIFs that have a global palette
  44. If pal Is Nothing Then
  45. Throw New Exception("Source GIF does not have a global palette.")
  46. End If
  47. '' Use color matrix to update the palette
  48. Using palBmp = New GcBitmap(pal, pal.Length, 1, True)
  49. palBmp.ApplyColorMatrix(colorMatrix)
  50. End Using
  51.  
  52. '' Set target paletter And other properties
  53. gw.GlobalPalette = pal
  54. gw.LogicalScreenWidth = gr.LogicalScreenWidth
  55. gw.LogicalScreenHeight = gr.LogicalScreenHeight
  56. gw.PixelAspectRatio = gr.PixelAspectRatio
  57. gw.AllowAddingTransparentColor = False
  58. Using tbmp = New GcBitmap()
  59. For i = 0 To gr.Frames.Count - 1
  60. Dim frame = gr.Frames(i)
  61. frame.ToGcBitmap(tbmp, i - 1)
  62.  
  63. '' Flip the image horizontally (this will reverse the 'rotation' of the fish):
  64. Using bmp = tbmp.FlipRotate(FlipRotateAction.FlipHorizontal)
  65. '' Apply the color matrix And append the frame to the target GIF:
  66. bmp.ApplyColorMatrix(colorMatrix)
  67. gw.AppendFrame(bmp, pal, DitheringMethod.NoDithering, 0, 0, GifDisposalMethod.DoNotDispose, frame.DelayTime, False)
  68. End Using
  69. Next
  70. End Using
  71. End Using
  72. ms.Seek(0, SeekOrigin.Begin)
  73. Return ms
  74. End Function
  75.  
  76. Public ReadOnly Property DefaultMime() As String
  77. Get
  78. Return MimeTypes.GIF
  79. End Get
  80. End Property
  81. End Class
  82.