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

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)
            ' Text layout for captions:
            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}

            ' Render all blend modes in a grid:
            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)

                ' Draw spectrum image normally:
                g.BlendMode = BlendMode.Normal
                g.DrawImage(ispectr, r, Nothing, ImageAlign.StretchImage)

                ' Draw blend mode name using the current blend mode:
                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)

                ' Current blend mode:
                g.BlendMode = blendMode
                g.DrawTextLayout(tl, New PointF(x, y))

                ' Draw spectrum image again using BlendMode.Difference
                ' to produce (mostly) colorful text on black background:
                g.BlendMode = BlendMode.Difference
                g.DrawImage(ispectr, r, Nothing, ImageAlign.StretchImage)

                ' Draw a rectangle to mark the current area:
                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

            ' For reference, draw the backdrop:
            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