HatchStyles.vb
''
'' 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 demonstrates the different hatch brush styles.
'' Many of those styles implement MS Excel pattern styles.
Public Class HatchStyles
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap

        '' HatchStyle
        Dim htchStyles = [Enum].GetValues(GetType(HatchStyle))
        Dim COLS = 4
        Dim w = pixelWidth / COLS
        Dim h = pixelHeight / ((htchStyles.Length + COLS - 1) / COLS)

        Dim tf = New TextFormat With
            {
                .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                .FontSize = 12
            }

        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = bmp.CreateGraphics(Color.White)
            Dim row = 0, col = 0
            For Each hs As HatchStyle In htchStyles
                '' Hatch:
                Dim rc = New RectangleF(col * w, row * h, w, h)
                Dim b = New HatchBrush(hs) With
                    {
                        .ForeColor = Color.Black,
                        .BackColor = Color.White
                    }
                g.FillRectangle(rc, b)
                '' Caption:
                Dim cap = hs.ToString()
                Dim scap = g.MeasureString(cap, tf)
                Dim rcap = New RectangleF(
                                    rc.X + (rc.Width - scap.Width) / 2,
                                    rc.Y + (rc.Height - scap.Height) / 2,
                                    scap.Width, scap.Height)
                rcap.Inflate(3, 2)
                g.FillRectangle(rcap, Color.White)
                g.DrawString(cap, tf, rcap, TextAlignment.Center, ParagraphAlignment.Center)
                '' Move on
                col += 1
                If col >= COLS Then
                    col = 0
                    row += 1
                End If
            Next
        End Using
        Return bmp
    End Function
End Class