RoundRectangle.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 System.Numerics
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Imaging
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' This sample demonstrates how to draw round rectangles
  15. '' using dedicated DrawRoundRect/FillRoundRect methods.
  16. '' It also shows how the same result may be achieved with
  17. '' graphics paths.
  18. Public Class RoundRectangle
  19. Function GenerateImage(
  20. ByVal pixelSize As Size,
  21. ByVal dpi As Single,
  22. ByVal opaque As Boolean,
  23. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  24.  
  25. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  26. Dim Inch = dpi
  27. Using g = bmp.CreateGraphics(Color.RoyalBlue)
  28. Dim rc = Util.AddNote(
  29. "GcGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
  30. "This sample also shows how the same result may be achieved using graphics paths. " +
  31. "While they are not really needed for drawing round rectangles, graphics paths allow " +
  32. "to draw and fill arbitrary figures with complex geometries.",
  33. g)
  34.  
  35. '' Rounded rectangle's radii:
  36. Dim rx = 36, ry = 24
  37.  
  38. '' Using dedicated methods to draw And fill round rectangles:
  39. Dim rEasy = New RectangleF(rc.Left, rc.Bottom + Inch / 2, Inch * 2, Inch)
  40. g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen)
  41. g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4)
  42. '' Add a label:
  43. Dim tf = New TextFormat() With
  44. {
  45. .Font = GCTEXT.Font.FromFile(IO.Path.Combine("Resources", "Fonts", "times.ttf")),
  46. .FontSize = Inch / 6
  47. }
  48. g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, False)
  49.  
  50. '' Using graphics path to achieve the same result:
  51. Dim rHard = rEasy
  52. rHard.Offset(0, rEasy.Height + Inch / 2)
  53. Dim path = MakeRoundRect(g, rHard, rx, ry)
  54. g.FillPath(path, Color.PaleVioletRed)
  55. g.DrawPath(path, Color.Purple, 4)
  56. '' Add a label:
  57. g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, False)
  58. End Using
  59. '' Done
  60. Return bmp
  61. End Function
  62.  
  63. '' This method shows how to create a graphics path that may be used
  64. '' to fill Or draw arbitrary shapes on a GcGraphics.
  65. Private Function MakeRoundRect(ByVal g As GcGraphics, ByVal rc As RectangleF, ByVal rx As Single, ByVal ry As Single) As IPath
  66. Dim path = g.CreatePath()
  67. Dim sz = New SizeF(rx, ry)
  68. '' start from horizontal top left
  69. path.BeginFigure(New PointF(rc.Left + rx, rc.Top))
  70. path.AddLine(New PointF(rc.Right - rx, rc.Top))
  71. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right, rc.Top + ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  72. path.AddLine(New PointF(rc.Right, rc.Bottom - ry))
  73. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Right - rx, rc.Bottom), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  74. path.AddLine(New PointF(rc.Left + rx, rc.Bottom))
  75. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left, rc.Bottom - ry), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  76. path.AddLine(New PointF(rc.Left, rc.Top + ry))
  77. path.AddArc(New ArcSegment() With {.Point = New PointF(rc.Left + rx, rc.Top), .SweepDirection = SweepDirection.Clockwise, .Size = sz})
  78. path.EndFigure(FigureEnd.Closed)
  79. Return path
  80. End Function
  81. End Class
  82.