SvgRandomArt.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.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GrapeCity.Documents.Svg;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17. using DsImagingWeb.Demos.Common;
  18.  
  19. namespace DsImagingWeb.Demos
  20. {
  21. // This sample generates some random abstract art using the SVG path API
  22. // provided by GcSvgDocument.
  23. public class SvgRandomArt
  24. {
  25. private System.Random rnd = Util.NewRandom();
  26.  
  27. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. using var svgDoc = new GcSvgDocument();
  30. var svg = svgDoc.RootSvg;
  31. svg.ViewBox = new SvgViewBox(0, 0, 100, 100);
  32.  
  33. // Random art:
  34. for (int i = 0; i < rnd.Next(6, 12); i++)
  35. AddFigure(svg, rnd);
  36.  
  37. // Border:
  38. svg.Children.Add(new SvgRectElement()
  39. {
  40. Width = new SvgLength(100),
  41. Height = new SvgLength(100),
  42. Stroke = new SvgPaint(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))),
  43. Fill = SvgPaint.None
  44. });
  45. // Clip:
  46. svg.Clip = new SvgClip(new SvgLength(0), new SvgLength(0), new SvgLength(100), new SvgLength(100));
  47.  
  48. // Render the SVG image:
  49. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  50. using var g = bmp.CreateGraphics(Color.White);
  51. var s = svgDoc.GetIntrinsicSize(SvgLengthUnits.Pixels);
  52.  
  53. var margin = dpi * 0.75f;
  54. var rect = new RectangleF(margin, margin, pixelSize.Width - margin * 2, pixelSize.Width - margin * 2);
  55. g.DrawSvg(svgDoc, rect);
  56.  
  57. // Done:
  58. return bmp;
  59. }
  60.  
  61. int Next100()
  62. {
  63. return rnd.Next(100);
  64. }
  65.  
  66. void AddFigure(SvgSvgElement svg, System.Random rnd)
  67. {
  68. var pb = new SvgPathBuilder();
  69. var startX = Next100();
  70. var startY = Next100();
  71. pb.AddMoveTo(false, startX, startY);
  72. for (int i = 0; i < rnd.Next(20, 80); ++i)
  73. AddCurve(pb, rnd);
  74. pb.AddCurveTo(false, Next100(), Next100(), Next100(), Next100(), startX, startY);
  75.  
  76. svg.Children.Add(new SvgPathElement()
  77. {
  78. FillRule = SvgFillRule.EvenOdd,
  79. Fill = new SvgPaint(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))),
  80. Stroke = new SvgPaint(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))),
  81. StrokeWidth = new SvgLength(rnd.Next(20) / 10f, SvgLengthUnits.Percentage),
  82. PathData = pb.ToPathData(),
  83. FillOpacity = Next100() / 100f,
  84. StrokeOpacity = Next100() / 100f,
  85. });
  86. }
  87.  
  88. void AddCurve(SvgPathBuilder pb, System.Random _rnd)
  89. {
  90. switch (_rnd.Next(5))
  91. {
  92. case 0:
  93. pb.AddCurveTo(false, Next100(), Next100(), Next100(), Next100(), Next100(), Next100());
  94. break;
  95. case 1:
  96. pb.AddSmoothCurveTo(false, Next100(), Next100(), Next100(), Next100());
  97. break;
  98. case 2:
  99. pb.AddQuadraticBezierCurveTo(false, Next100(), Next100(), Next100(), Next100());
  100. break;
  101. case 3:
  102. pb.AddSmoothQuadraticBezierCurveTo(false, Next100(), Next100());
  103. break;
  104. case 4:
  105. pb.AddEllipticalArc(false, Next100(), Next100(), _rnd.Next(360),
  106. _rnd.Next(2) == 0 ? GCDRAW.ArcSize.Small : GCDRAW.ArcSize.Large,
  107. _rnd.Next(2) == 0 ? GCDRAW.SweepDirection.Clockwise : GCDRAW.SweepDirection.CounterClockwise,
  108. Next100(), Next100());
  109. break;
  110. }
  111. }
  112. }
  113. }
  114.