ImageTypesInTiff.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 takes a color JPEG, converts it to different image types
'' (such as b/w and grayscale) and creates a multi-frame TIFF from those
'' images.
Public Class ImageTypesInTiff
    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.Tiff Then
            Throw New Exception("This sample only supports TIFF output format.")
        End If

        Dim pth = Path.Combine("Resources", "Images", "minerva.jpg")

        Dim ms = New MemoryStream()
        Using tw = New GcTiffWriter(ms),
            bmp = New GcBitmap(pth)
            '' Using the green channel produces a marginally better result for this photo.
            '' For a much better bilevel image produced by dithering, see DitheringInTiff.
            Using f = bmp.ToBilevelBitmap(ColorChannel.Green)
                tw.AppendFrame(f)
            End Using
            Using f = bmp.ToGrayscaleBitmap()
                tw.AppendFrame(f)
            End Using
            '' 4bpp allows from 8 to 16 colors, we use max (16)
            Using f = bmp.ToIndexed4bppBitmap(16)
                tw.AppendFrame(f)
            End Using
            '' 8bpp allows from 8 to 256 colors, we use max (256)
            Using f = bmp.ToIndexed8bppBitmap(256)
                tw.AppendFrame(f)
            End Using
            tw.AppendFrame(bmp)
        End Using
        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function

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