DitheringInTiff.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
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 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 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.TIFF Then
            Throw New Exception("This sample only supports TIFF output format.")
        End If

        Dim pth = Path.Combine("Resources", "Images", "minerva.jpg")
        Dim fnt = GCTEXT.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 DefaultMime() As String
        Get
            Return MimeTypes.TIFF
        End Get
    End Property
End Class