RoundRectangle.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.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsImagingWeb.Demos
  15. {
  16. // This sample demonstrates how to draw round rectangles
  17. // using dedicated DrawRoundRect/FillRoundRect methods.
  18. // It also shows how the same result may be achieved with
  19. // graphics paths.
  20. public class RoundRectangle
  21. {
  22. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  23. {
  24. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  25. var Inch = dpi;
  26. using (var g = bmp.CreateGraphics(Color.RoyalBlue))
  27. {
  28. var rc = Common.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. float rx = 36, ry = 24;
  37.  
  38. // Using dedicated methods to draw and fill round rectangles:
  39. var 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. var tf = new TextFormat()
  44. {
  45. Font = GCTEXT.Font.FromFile(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. var rHard = rEasy;
  52. rHard.Offset(0, rEasy.Height + Inch / 2);
  53. var 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.  
  59. // Done:
  60. }
  61. // Done:
  62. return bmp;
  63. }
  64.  
  65. // This method shows how to create a graphics path that may be used
  66. // to fill or draw arbitrary shapes on a GcGraphics.
  67. private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
  68. {
  69. var path = g.CreatePath();
  70. var sz = new SizeF(rx, ry);
  71. // start from horizontal top left
  72. path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
  73. path.AddLine(new PointF(rc.Right - rx, rc.Top));
  74. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  75. path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
  76. path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
  77. path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
  78. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
  79. path.AddLine(new PointF(rc.Left, rc.Top + ry));
  80. path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
  81. path.EndFigure(FigureEnd.Closed);
  82. return path;
  83. }
  84. }
  85. }
  86.