GraphicsTransforms.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Numerics;
  9. using GrapeCity.Documents.Drawing;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Imaging;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsImagingWeb.Demos
  16. {
  17. // Shows how to use graphics transformations (GcGraphics.Transform property).
  18. public class GraphicsTransforms
  19. {
  20. private void DrawBox(string text, GcGraphics g, RectangleF box)
  21. {
  22. g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204));
  23. g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1);
  24. box.Inflate(-6, -6);
  25. g.DrawString(text, new TextFormat()
  26. {
  27. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  28. FontSize = 14,
  29. },
  30. box);
  31. }
  32.  
  33. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  34. {
  35. const string baseTxt = "Text drawn at (0,36) in a 4\"x2\" box";
  36. var Inch = dpi;
  37.  
  38. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  39. using (var g = bmp.CreateGraphics(Color.White))
  40. {
  41. var box = new RectangleF(0, 36, dpi * 4, dpi * 2);
  42. // #1:
  43. DrawBox($"Box 1: {baseTxt}, no transformations.", g, box);
  44. //
  45. var translate0 = Matrix3x2.CreateTranslation(Inch * 1, Inch * 4);
  46. var scale0 = Matrix3x2.CreateScale(0.5f);
  47. //
  48. // Transforms are applied in order from last to first.
  49. // #2:
  50. g.Transform =
  51. scale0 *
  52. translate0;
  53. DrawBox($"Box 2: {baseTxt}, translated by (1\",4\") and scaled by 0.5.", g, box);
  54. // #3:
  55. g.Transform =
  56. translate0 *
  57. scale0;
  58. DrawBox($"Box 3: {baseTxt}, scaled by 0.5 and translated by (1\",4\").", g, box);
  59. //
  60. var translate1 = Matrix3x2.CreateTranslation(Inch * 3, Inch * 5);
  61. var scale1 = Matrix3x2.CreateScale(0.7f);
  62. var rotate0 = Matrix3x2.CreateRotation((float)(-70 * Math.PI) / 180f); // 70 degrees CCW
  63. // #4:
  64. g.Transform =
  65. rotate0 *
  66. translate1 *
  67. scale1;
  68. DrawBox($"Box 4: {baseTxt}, scaled by 0.7, translated by (3\",5\"), and rotated 70 degrees counterclockwise.", g, box);
  69. // #5:
  70. g.Transform =
  71. Matrix3x2.CreateTranslation(36, Inch) *
  72. g.Transform;
  73. DrawBox($"Box 5: {baseTxt}, applied current transform (Box 4), and translated by (1/2\",1\").", g, box);
  74. // #6:
  75. g.Transform =
  76. // rotate0 *
  77. Matrix3x2.CreateSkew((float)(-45 * Math.PI) / 180f, (float)(20 * Math.PI) / 180f) *
  78. Matrix3x2.CreateTranslation(Inch * 3, Inch * 6);
  79. DrawBox($"Box 6: {baseTxt}, translated by (3\",6\"), and skewed -45 degrees on axis X and 20 degrees on axis Y.", g, box);
  80. // #7:
  81. g.Transform =
  82. Matrix3x2.CreateRotation((float)Math.PI) *
  83. Matrix3x2.CreateTranslation(bmp.Width - dpi, bmp.Height - dpi);
  84. DrawBox($"Box 7: {baseTxt}, translated by (7.5\",10\"), and rotated by 180 degrees.", g, box);
  85. // We can remove any transformations on a graphics like so:
  86. g.Transform = Matrix3x2.Identity;
  87. // Draw border around the whole image:
  88. g.DrawRectangle(new RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4);
  89. }
  90. // Done:
  91. return bmp;
  92. }
  93. }
  94. }
  95.