GraphicsTransforms.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. '' Shows how to use graphics transformations (GcGraphics.Transform property).
  15. Public Class GraphicsTransforms
  16.  
  17. Private Sub DrawBox(ByVal text As String, ByVal g As GcGraphics, ByVal box As RectangleF)
  18. g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204))
  19. g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1)
  20. box.Inflate(-6, -6)
  21. g.DrawString(text, New TextFormat() With
  22. {
  23. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  24. .FontSize = 14
  25. },
  26. box)
  27. End Sub
  28.  
  29. Function GenerateImage(
  30. ByVal pixelSize As Size,
  31. ByVal dpi As Single,
  32. ByVal opaque As Boolean,
  33. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  34.  
  35. Const baseTxt = "Text drawn at (0,36) in a 4""x2"" box"
  36. Dim Inch = dpi
  37.  
  38. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  39. Using g = bmp.CreateGraphics(Color.White)
  40. Dim box = New RectangleF(0, 36, dpi * 4, dpi * 2)
  41. '' #1:
  42. DrawBox($"Box 1: {baseTxt}, no transformations.", g, box)
  43. ''
  44. Dim translate0 = Matrix3x2.CreateTranslation(Inch * 1, Inch * 4)
  45. Dim scale0 = Matrix3x2.CreateScale(0.5F)
  46. ''
  47. '' Transforms are applied in order from last to first.
  48. '' #2:
  49. g.Transform =
  50. scale0 *
  51. translate0
  52. DrawBox($"Box 2: {baseTxt}, translated by (1"", 4"") and scaled by 0.5.", g, box)
  53. '' #3:
  54. g.Transform =
  55. translate0 *
  56. scale0
  57. DrawBox($"Box 3: {baseTxt}, scaled by 0.5 and translated by (1"", 4"").", g, box)
  58. ''
  59. Dim translate1 = Matrix3x2.CreateTranslation(Inch * 3, Inch * 5)
  60. Dim scale1 = Matrix3x2.CreateScale(0.7F)
  61. Dim rotate0 = Matrix3x2.CreateRotation((-70 * Math.PI) / 180.0F) '' 70 degrees CCW
  62. '' #4:
  63. g.Transform =
  64. rotate0 *
  65. translate1 *
  66. scale1
  67. DrawBox($"Box 4: {baseTxt}, scaled by 0.7, translated by (3"", 5""), and rotated 70 degrees counterclockwise.", g, box)
  68. '' #5:
  69. g.Transform =
  70. Matrix3x2.CreateTranslation(36, Inch) *
  71. g.Transform
  72. DrawBox($"Box 5: {baseTxt}, applied current transform (Box 4), and translated by (1/2"", 1"").", g, box)
  73. '' #6:
  74. g.Transform =
  75. Matrix3x2.CreateSkew((-45 * Math.PI) / 180.0F, (20 * Math.PI) / 180.0F) *
  76. Matrix3x2.CreateTranslation(Inch * 3, Inch * 6)
  77. DrawBox($"Box 6: {baseTxt}, translated by (3"", 6""), and skewed -45 degrees on axis X and 20 degrees on axis Y.", g, box)
  78. '' #7:
  79. g.Transform =
  80. Matrix3x2.CreateRotation(Math.PI) *
  81. Matrix3x2.CreateTranslation(bmp.Width - dpi, bmp.Height - dpi)
  82. DrawBox($"Box 7: {baseTxt}, translated by (7.5"", 10""), and rotated by 180 degrees.", g, box)
  83. '' We can remove any transformations on a graphics like so:
  84. g.Transform = Matrix3x2.Identity
  85. '' Draw border around the whole image:
  86. g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
  87. End Using
  88. '' Done
  89. Return bmp
  90. End Function
  91. End Class
  92.