DitheringInTiff.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © 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

'' このサンプルでは、カラー JPEG 画像から利用可能なすべてのディザリング方式を使用して
'' 2 値ビットマップを生成します。
'' 生成された白黒画像は、マルチフレーム TIFF の各フレームとして追加されます。
'' 元のカラー画像は、最後のページとして追加されます。
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", "FreeMono.ttf"))
        Dim ms = New MemoryStream()
        Using tw = New GcTiffWriter(ms), bmp = New GcBitmap(pth)
            '' ラベル用のテキストレイアウトを作成します。
            Dim tl = New TextLayout(bmp.DpiX)
            tl.DefaultFormat.Font = fnt
            tl.DefaultFormat.FontSize = 16
            tl.DefaultFormat.BackColor = Color.White
            '' すべてのディザリング手法をループで処理します(ディザリングなしから開始します)。
            Dim methods = GetType(DitheringMethod).GetEnumValues()
            For Each method As DitheringMethod In methods
                Using tbmp = bmp.Clone()
                    '' ディザリングを適用します。
                    tbmp.ApplyEffect(DitheringEffect.Get(method))
                    '' ラベルを描画します。
                    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
                    '' 2値(白黒)ビットマップに変換します。
                    Using f = tbmp.ToBilevelBitmap()
                        tw.AppendFrame(f)
                    End Using
                End Using
            Next
            '' 元の画像を追加します。
            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