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

'' Shows how to use graphics transformations (GcGraphics.Transform property).
Public Class GraphicsTransforms

    Private Sub DrawBox(ByVal text As String, ByVal g As GcGraphics, ByVal box As RectangleF)
        g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204))
        g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1)
        box.Inflate(-6, -6)
        g.DrawString(text, New TextFormat() With
            {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                .FontSize = 14
            },
            box)
    End Sub

    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Const baseTxt = "Text drawn at (0,36) in a 4""x2"" box"
        Dim Inch = dpi

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        Using g = bmp.CreateGraphics(Color.White)
            Dim box = New RectangleF(0, 36, dpi * 4, dpi * 2)
            '' #1:
            DrawBox($"Box 1: {baseTxt}, no transformations.", g, box)
            ''
            Dim translate0 = Matrix3x2.CreateTranslation(Inch * 1, Inch * 4)
            Dim scale0 = Matrix3x2.CreateScale(0.5F)
            ''
            '' Transforms are applied in order from last to first.
            '' #2:
            g.Transform =
                scale0 *
                translate0
            DrawBox($"Box 2: {baseTxt}, translated by (1"", 4"") and scaled by 0.5.", g, box)
            '' #3:
            g.Transform =
                translate0 *
                scale0
            DrawBox($"Box 3: {baseTxt}, scaled by 0.5 and translated by (1"", 4"").", g, box)
            ''
            Dim translate1 = Matrix3x2.CreateTranslation(Inch * 3, Inch * 5)
            Dim scale1 = Matrix3x2.CreateScale(0.7F)
            Dim rotate0 = Matrix3x2.CreateRotation((-70 * Math.PI) / 180.0F) '' 70 degrees CCW
            '' #4:
            g.Transform =
                rotate0 *
                translate1 *
                scale1
            DrawBox($"Box 4: {baseTxt}, scaled by 0.7, translated by (3"", 5""), and rotated 70 degrees counterclockwise.", g, box)
            '' #5:
            g.Transform =
                Matrix3x2.CreateTranslation(36, Inch) *
                g.Transform
            DrawBox($"Box 5: {baseTxt}, applied current transform (Box 4), and translated by (1/2"", 1"").", g, box)
            '' #6:
            g.Transform =
                Matrix3x2.CreateSkew((-45 * Math.PI) / 180.0F, (20 * Math.PI) / 180.0F) *
                Matrix3x2.CreateTranslation(Inch * 3, Inch * 6)
            DrawBox($"Box 6: {baseTxt}, translated by (3"", 6""), and skewed -45 degrees on axis X and 20 degrees on axis Y.", g, box)
            '' #7:
            g.Transform =
                Matrix3x2.CreateRotation(Math.PI) *
                Matrix3x2.CreateTranslation(bmp.Width - dpi, bmp.Height - dpi)
            DrawBox($"Box 7: {baseTxt}, translated by (7.5"", 10""), and rotated by 180 degrees.", g, box)
            '' We can remove any transformations on a graphics like so:
            g.Transform = Matrix3x2.Identity
            '' Draw border around the whole image:
            g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
        End Using
        '' Done
        Return bmp
    End Function
End Class