Gradients.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Drawing
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Imaging
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
  14. Public Class Gradients
  15. Function GenerateImage(
  16. ByVal pixelSize As Size,
  17. ByVal dpi As Single,
  18. ByVal opaque As Boolean,
  19. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  20.  
  21. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  22. Dim Inch = dpi
  23. Using g = bmp.CreateGraphics(Color.RoyalBlue)
  24. Dim testRectSize = New SizeF(Inch * 6, Inch)
  25. Dim dy = Inch / 6
  26. '' TextLayout to draw labels:
  27. Dim tl = g.CreateTextLayout()
  28. tl.DefaultFormat.FontSize = Inch / 6
  29. tl.DefaultFormat.ForeColor = Color.Chartreuse
  30. tl.MaxWidth = testRectSize.Width
  31. tl.MaxHeight = testRectSize.Height
  32. tl.TextAlignment = TextAlignment.Center
  33. tl.ParagraphAlignment = ParagraphAlignment.Center
  34. '' Note 1:
  35. Dim rc = Util.AddNote("Linear gradients using LinearGradientBrush:", g, New RectangleF(Inch, Inch / 2, 500, 100))
  36. '' Text insertion point:
  37. Dim ip = New PointF(rc.Left, rc.Bottom + dy)
  38. '' Local action to draw a gradient-filled rectangle:
  39. Dim drawSwatch As Action(Of GCDRAW.Brush, String) =
  40. Sub(b_, txt_)
  41. Dim rect = New RectangleF(ip, testRectSize)
  42. '' Fill the rectangle with a gradient brush:
  43. g.FillRectangle(rect, b_)
  44. '' Draw a border, text info etc:
  45. g.DrawRectangle(rect, Color.Magenta)
  46. tl.Clear()
  47. tl.Append(txt_)
  48. tl.MaxHeight = testRectSize.Height
  49. tl.MaxWidth = testRectSize.Width
  50. tl.PerformLayout(True)
  51. g.DrawTextLayout(tl, ip)
  52. ip.Y += rect.Height + dy
  53. End Sub
  54.  
  55. '' LinearGradientBrush:
  56. '' Horizontal gradient:
  57. Dim linearGradBrush = New LinearGradientBrush(Color.Red, Color.Blue)
  58. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  59. '' Vertical gradient:
  60. linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Green, New PointF(0, 1))
  61. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  62. '' Diagonal gradient (increase swatch height to better show diagonal):
  63. testRectSize.Height *= 2
  64. linearGradBrush = New LinearGradientBrush(Color.Red, New PointF(0, 0), Color.Teal, New PointF(1, 1))
  65. drawSwatch(linearGradBrush, $"Linear gradient{vbCrLf}from {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}")
  66. '' RadialGradientBrush
  67. rc = Util.AddNote("Radial gradients using RadialGradientBrush:", g, New RectangleF(ip, New SizeF(500, 100)))
  68. ip.Y = rc.Bottom + dy
  69. '' Centered:
  70. '' testRectSize.Height *= 2
  71. Dim radialGradBrush = New RadialGradientBrush(Color.Orange, Color.Purple)
  72. drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
  73. '' Center in bottom right corner:
  74. radialGradBrush = New RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, New PointF(1, 1))
  75. drawSwatch(radialGradBrush, $"Radial gradient{vbCrLf}with origin at {radialGradBrush.GradientOrigin}")
  76. End Using
  77. '' Done
  78. Return bmp
  79. End Function
  80. End Class
  81.