BlendText.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
'' 
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' このサンプルは、GcGraphics で描画を行う際に異なるブレンドモードを
'' 指定した際の効果を示しています。GcGraphics (GcBitmapGraphics を含む) 
'' のインスタンスに設定された現在のブレンドモードは、画像だけでなく
'' すべての描画操作に影響することに注意してください。
'' BlendImages_0 および BlendingModes のサンプルも参照してください。
Public Class BlendText
    Function GenerateImage(
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim ispectr = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-500x500.png"))
        Const margin As Integer = 36
        Const NCOLS As Integer = 4
        Dim w As Integer = CInt((pixelSize.Width - margin * 2) / NCOLS)
        Dim h As Integer = w \ 2
        Dim row As Integer = 0, col As Integer = 0
        Dim bottomy As Single = 0.0F

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)
            ' 説明文用のテキストレイアウトを設定します。
            Dim tl = g.CreateTextLayout()
            tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMonoBold.ttf"))
            tl.DefaultFormat.FontSize = 22
            tl.ParagraphAlignment = ParagraphAlignment.Center
            tl.TextAlignment = TextAlignment.Center
            tl.MaxWidth = w
            tl.MaxHeight = h
            tl.MarginTop = CSng((h - h / 1.4F) / 2.0F)
            tl.DefaultFormat.ForeColor = Color.Black
            Dim tfWhite = New TextFormat(tl.DefaultFormat) With {.ForeColor = Color.White}

            ' すべてのブレンドモードをグリッド状に描画します。
            For Each mode In [Enum].GetValues(GetType(BlendMode))
                Dim blendMode As BlendMode = CType(mode, BlendMode)
                If Not g.IsBlendModeSupported(blendMode) Then Continue For

                Dim x As Integer = margin + w * col
                Dim y As Integer = margin + h * row
                Dim r = New RectangleF(x, y, w, h)

                ' スペクトル画像を通常モード(Normal)で描画します。
                g.BlendMode = BlendMode.Normal
                g.DrawImage(ispectr, r, Nothing, ImageAlign.StretchImage)

                ' 現在のブレンドモードを使用してブレンドモード名を描画します。
                tl.Clear()
                tl.AppendLine("B: " & blendMode.ToString())
                tl.Append("W: " & blendMode.ToString(), tfWhite)
                tl.PerformLayout(True)
                Dim rc = tl.ContentRectangle
                rc.Offset(x, y)
                rc.Inflate(4, 2)

                ' 現在のブレンドモードを設定します。
                g.BlendMode = blendMode
                g.DrawTextLayout(tl, New PointF(x, y))

                ' 背景を黒くし、テキストを(ほぼ)カラフルに表示させるため、
                ' BlendMode.Difference を使用してスペクトル画像を再度描画します。
                g.BlendMode = BlendMode.Difference
                g.DrawImage(ispectr, r, Nothing, ImageAlign.StretchImage)

                ' 現在の領域を示す矩形を描画します。
                g.BlendMode = BlendMode.Normal
                g.DrawRectangle(r, Color.Gray)

                bottomy = r.Bottom
                col += 1
                If col = NCOLS Then
                    col = 0
                    row += 1
                End If
            Next

            ' 参考用として、背景に使用した画像を描画します。
            tl.Clear()
            tl.MarginAll = 0
            tl.MaxWidth = bmp.PixelWidth
            tl.MaxHeight = Nothing
            tl.DefaultFormat.FontSize = 14
            tl.TextAlignment = TextAlignment.Leading
            tl.ParagraphAlignment = ParagraphAlignment.Near
            tl.Append("The spectrum image used as the backdrop for the texts above:")
            g.DrawTextLayout(tl, New PointF(margin, bottomy + margin))
            g.DrawImage(ispectr,
                        New RectangleF(margin, bottomy + margin + tl.ContentHeight + 24, w, w),
                        Nothing, ImageAlign.StretchImage)
        End Using

        Return bmp
    End Function
End Class