'' 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,
                ByVal pixelSize As Size,
                ByVal dpi As Single,
                ByVal opaque As Boolean,
                Optional sampleParams As String() = Nothing) 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:
                    '' Draw label:
                    Using g = tbmp.CreateGraphics()
                        g.DrawTextLayout(tl, New PointF(0, tbmp.Height - tl.ContentHeight))
                    End Using
                    '' Convert to bilevel bitmap
                    Using f = tbmp.ToBilevelBitmap()
                    End Using
                End Using
            '' Add original image:
        End Using
        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function

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