SvgGraphicsRoundRectangle.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 GrapeCity.Documents.Imaging;
  9. using GrapeCity.Documents.Svg;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsImagingWeb.Demos
  16. {
  17. // This example shows how to draw on GcSvgGraphics.
  18. // It draws round rectangles using both dedicated DrawRoundRect/FillRoundRect
  19. // methods and graphics paths.
  20. // Note that the code in this sample is identical to the drawing code
  21. // in the RoundRectangle sample, but creates a vector SVG image
  22. // rather than a raster GcBitmap.
  23. public class SvgGraphicsRoundRectangle
  24. {
  25. public string DefaultMime { get => Common.Util.MimeTypes.SVG; }
  26.  
  27. public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. if (targetMime != Common.Util.MimeTypes.SVG)
  30. throw new Exception("This sample only supports SVG output format.");
  31.  
  32. var Inch = dpi;
  33. var ms = new MemoryStream();
  34. using var g = new GcSvgGraphics(pixelSize.Width, pixelSize.Height);
  35. if (opaque)
  36. g.FillRectangle(new RectangleF(0, 0, g.Width, g.Height), Color.White);
  37.  
  38. var rc = Common.Util.AddNote(
  39. "GcGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
  40. "This sample also shows how the same result may be achieved using graphics paths. " +
  41. "While they are not really needed for drawing round rectangles, graphics paths allow " +
  42. "to draw and fill arbitrary figures with complex geometries.\r\n\r\n" +
  43. "Note that this version of the \"Round Rectangles\" sample draws on a GcSvgGraphics, " +
  44. "and produces a vector SVG image rather than a raster image (JPEG, PNG etc.).",
  45. g);
  46.  
  47. // Rounded rectangle's radii:
  48. float rx = 36, ry = 24;
  49.  
  50. // Using dedicated methods to draw and fill round rectangles:
  51. var rEasy = new RectangleF(rc.Left, rc.Bottom + Inch / 2, Inch * 2, Inch);
  52. g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen);
  53. g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4);
  54. // Add a label:
  55. var tf = new TextFormat()
  56. {
  57. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  58. FontSize = Inch / 6
  59. };
  60. g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, false);
  61.  
  62. // Using graphics path to achieve the same result:
  63. var rHard = rEasy;
  64. rHard.Offset(0, rEasy.Height + Inch / 2);
  65. var path = MakeRoundRect(g, rHard, rx, ry);
  66. g.FillPath(path, Color.PaleVioletRed);
  67. g.DrawPath(path, Color.Purple, 4);
  68. // Add a label:
  69. g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, false);
  70.  
  71. // Done:
  72. var svg = g.ToSvgDocument();
  73. svg.Save(ms);
  74. ms.Seek(0, SeekOrigin.Begin);
  75. return ms;
  76. }
  77.  
  78. // This method shows how to create a graphics path that may be used
  79. // to fill or draw arbitrary shapes on a GcGraphics.
  80. private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
  81. {
  82. var path = g.CreatePath();
  83. var sz = new SizeF(rx, ry);
  84. // start from horizontal top left
  85. path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
  86. path.AddLine(new PointF(rc.Right - rx, rc.Top));
  87. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  88. path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
  89. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
  90. path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
  91. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  92. path.AddLine(new PointF(rc.Left, rc.Top + ry));
  93. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
  94. path.EndFigure(FigureEnd.Closed);
  95. return path;
  96. }
  97. }
  98. }
  99.