MakeGif.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample loads the image of a goldfish (same as the one used in BmpTransforms)
'' and applies different transformations to it, creating a number of frames that
'' are combined to produce an animated GIF.
Public Class MakeGif
    Function GenerateImageStream(
                ByVal targetEncoding As ImageEncoding,
                Optional pixelWidth As Integer = 1024,
                Optional pixelHeight As Integer = 1024,
                Optional opaque As Boolean = True,
                Optional dpiX As Single = 96,
                Optional dpiY As Single = 96) As Stream

        If Not targetEncoding = ImageEncoding.Gif Then
            Throw New Exception("This sample only supports GIF output format.")
        End If

        '' Keep GIF size reasonable:
        Dim side2 = Math.Min(400, Math.Min(pixelWidth, pixelHeight))

        '' Prepare frames for the target GIF flipping/rotating a single image:
        Dim move1() As GcBitmap = New GcBitmap(2) {}
        Dim move2() As GcBitmap = New GcBitmap(3) {}
        Using bmpSrc = New GcBitmap(Path.Combine("Resources", "Stock", "goldfish.jpg"))
            bmpSrc.Opaque = opaque
            '' Adjust straight And flipped images to try And keep the fish's head motionless:
            Using tbmp = bmpSrc.Resize(side2, side2)
                move1(0) = New GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY)
                move1(0).Clear(Color.White)
                move1(0).BitBlt(tbmp, -CType(side2 / 14.0F, Integer), 0)
            End Using
            Using tbmp = move1(0).FlipRotate(FlipRotateAction.FlipHorizontal)
                move1(1) = New GcBitmap(tbmp.PixelWidth, tbmp.PixelHeight, tbmp.Opaque, tbmp.DpiX, tbmp.DpiY)
                move1(1).Clear(Color.White)
                move1(1).BitBlt(tbmp, -CType(side2 / 14.0F, Integer), 0)
                move1(1).BitBlt(tbmp, 0, 0)
            End Using
            move2(0) = move1(0).FlipRotate(FlipRotateAction.Rotate90)
            move2(1) = move1(0).FlipRotate(FlipRotateAction.Rotate180)
            move2(2) = move1(0).FlipRotate(FlipRotateAction.Rotate270)
        End Using
        '' Combine the moves:
        Dim bmps = New List(Of GcBitmap)()
        For i = 0 To 3
            bmps.Add(move1(0))
            bmps.Add(move1(1))
        Next
        bmps.Add(move1(0))
        For i = 0 To 2
            bmps.Add(move1(0))
            bmps.Add(move2(0))
            bmps.Add(move2(1))
            bmps.Add(move2(2))
        Next
        bmps.Add(move1(0))
        '' Create the GIF:
        Dim ms = New MemoryStream()
        Using gw = New GcGifWriter(ms)
            gw.LogicalScreenWidth = bmps(0).PixelWidth
            gw.LogicalScreenHeight = bmps(0).PixelHeight
            gw.PixelAspectRatio = 1
            gw.AllowAddingTransparentColor = False

            For Each bmp In bmps
                gw.AppendFrame(bmp, 255, 0, 0, GifDisposalMethod.DoNotDispose, 16)
            Next
        End Using
        '' Dispose bitmaps used to create GIF frames:
        For Each bmp In bmps.Distinct()
            bmp.Dispose()
        Next

        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function

    Public ReadOnly Property DefaultEncoding() As ImageEncoding
        Get
            Return ImageEncoding.Gif
        End Get
    End Property
End Class