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

'' Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
Public Class Gradients
    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
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Dim Inch = dpiX
        Using g = bmp.CreateGraphics(Color.RoyalBlue)
            Dim testRectSize = New SizeF(Inch * 6, Inch)
            Dim dy = Inch / 6
            '' TextLayout to draw labels:
            Dim tl = g.CreateTextLayout()
            ''qq tl.DefaultFormat.Font = StandardFonts.Times
            tl.DefaultFormat.FontSize = Inch / 6
            tl.DefaultFormat.ForeColor = Color.Chartreuse
            tl.MaxWidth = testRectSize.Width
            tl.MaxHeight = testRectSize.Height
            tl.TextAlignment = TextAlignment.Center
            tl.ParagraphAlignment = ParagraphAlignment.Center
            '' Note 1:
            Dim rc = Util.AddNote("Linear gradients using LinearGradientBrush:", g, New RectangleF(Inch, Inch / 2, 500, 100))
            '' Text insertion point:
            Dim ip = New PointF(rc.Left, rc.Bottom + dy)
            '' Local action to draw a gradient-filled rectangle:
            Dim drawSwatch As Action(Of Brush, String) =
                Sub(b_, txt_)
                    Dim rect = New RectangleF(ip, testRectSize)
                    '' Fill the rectangle with a gradient brush:
                    g.FillRectangle(rect, b_)
                    '' Draw a border, text info etc:
                    g.DrawRectangle(rect, Color.Magenta)
                    tl.Clear()
                    tl.Append(txt_)
                    tl.MaxHeight = testRectSize.Height
                    tl.MaxWidth = testRectSize.Width
                    tl.PerformLayout(True)
                    g.DrawTextLayout(tl, ip)
                    ip.Y += rect.Height + dy
                End Sub

            '' LinearGradientBrush:
            '' Horizontal gradient:
            Dim linearGradBrush = New LinearGradientBrush(Color.Red, Color.Blue)
            drawSwatch(linearGradBrush, $"Linear gradient\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
            '' Vertical gradient:
            linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Green, New PointF(0, 1))
            drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
            '' Diagonal gradient (increase swatch height to better show diagonal):
            testRectSize.Height *= 2
            linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Teal, New PointF(1, 1))
            drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
            '' RadialGradientBrush
            rc = Util.AddNote("Radial gradients using RadialGradientBrush:", g, New RectangleF(ip, New SizeF(500, 100)))
            ip.Y = rc.Bottom + dy
            '' Centered:
            '' testRectSize.Height *= 2
            Dim radialGradBrush = New RadialGradientBrush(Color.Orange, Color.Purple)
            drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}")
            '' Center in bottom right corner:
            radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
            drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}")
        End Using
        '' Done
        Return bmp
    End Function
End Class