Gradients.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

'' Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
Public Class Gradients
    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        Dim Inch = dpi
        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()
            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 GCDRAW.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{vbCrLf}from {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{vbCrLf}from {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{vbCrLf}from {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{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
            '' Center in bottom right corner:
            radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
            drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
        End Using
        '' Done
        Return bmp
    End Function
End Class