DitheringInTiff.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, And creates bilevel bitmaps from it
'' using all available dithering methods. The resulting b/w images
'' are added as frames to a multi-frame TIFF.
'' The original color image Is added as the last page.
Public Class DitheringInTiff
    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 fnt = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
        Dim ms = New MemoryStream()
        Using tw = New GcTiffWriter(ms), bmp = New GcBitmap(pth)
            '' Create text layout for labels:
            Dim tl = New TextLayout(bmp.DpiX)
            tl.DefaultFormat.Font = fnt
            tl.DefaultFormat.FontSize = 16
            tl.DefaultFormat.BackColor = Color.White
            '' Loop through all dithering methods (starting with no dithering):
            Dim methods = GetType(DitheringMethod).GetEnumValues()
            For Each method As DitheringMethod In methods
                Using tbmp = bmp.Clone()
                    '' Apply dithering:
                    tbmp.ApplyEffect(DitheringEffect.Get(method))
                    '' Draw label:
                    tl.Clear()
                    tl.Append(method.ToString())
                    tl.PerformLayout(True)
                    Using g = tbmp.CreateGraphics()
                        g.DrawTextLayout(tl, New PointF(0, tbmp.Height - tl.ContentHeight))
                    End Using
                    '' Convert to bilevel bitmap
                    Using f = tbmp.ToBilevelBitmap()
                        tw.AppendFrame(f)
                    End Using
                End Using
            Next
            '' Add original image:
            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