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

'' 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 = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                .FontSize = 14
            },
            box)
    End Sub

    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

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

        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = bmp.CreateGraphics(Color.White)
            Dim box = New RectangleF(0, 36, dpiX * 4, dpiY * 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 - dpiX, bmp.Height - dpiY)
            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