MakeGif.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS 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 targetMime As String,
                ByVal pixelSize As Size,
                ByVal dpi As Single,
                ByVal opaque As Boolean,
                Optional sampleParams As String() = Nothing) As Stream

        If Not targetMime = MimeTypes.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(pixelSize.Width, pixelSize.Height))

        '' 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 DefaultMime() As String
        Get
            Return MimeTypes.GIF
        End Get
    End Property
End Class