SvgClipArt.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. // Use GcSvgDocument to rasterize and render SVG files to raster images.
  22. // The SVG clip art used in this sample is from freesvg.org.
  23. public class SvgClipArt
  24. {
  25. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  26. {
  27. const int rows = 4;
  28. const int cols = 4;
  29. float margin = dpi / 2;
  30. float sMargin = margin / 4;
  31.  
  32. // Load images from the resources folder:
  33. List<string> fnames = new List<string>(Directory.GetFiles(Path.Combine("Resources", "SvgClipArt"), "*", SearchOption.AllDirectories));
  34. fnames.Shuffle();
  35. var images = new List<(string, GcSvgDocument)>();
  36. foreach (var fname in fnames.Take(rows * cols))
  37. images.Add((Path.GetFileName(fname), GcSvgDocument.FromFile(fname)));
  38.  
  39. // Font and format for captions:
  40. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
  41. var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
  42.  
  43. // Set up a 4x4 layout grid:
  44. float gapx = margin / 4, gapy = gapx;
  45. float sWidth = (pixelSize.Width - margin * 2 + gapx) / cols;
  46. float sHeight = (pixelSize.Height - margin * 2 + gapy) / rows;
  47. if (sWidth > sHeight)
  48. {
  49. gapx += sWidth - sHeight;
  50. sWidth = sHeight;
  51. }
  52. else
  53. {
  54. gapy += sHeight - sWidth;
  55. sHeight = sWidth;
  56. }
  57. var ip = new PointF(margin, margin);
  58.  
  59. // Resulting bitmap:
  60. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  61. // Render images that fit within the grid:
  62. using var g = bmp.CreateGraphics(Color.White);
  63. for (int i = 0; i < images.Count(); ++i)
  64. {
  65. // Draw border around image:
  66. var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
  67. g.FillRectangle(rect, Color.LightGray);
  68. g.DrawRectangle(rect, Color.Black, 0.5f);
  69. rect.Inflate(-sMargin, -sMargin);
  70. // Print image file name as caption:
  71. g.DrawString(Path.GetFileName(images[i].Item1), tf,
  72. new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  73. TextAlignment.Center, ParagraphAlignment.Near, false);
  74. // Draw the SVG:
  75. var svg = images[i].Item2;
  76. var s = svg.GetIntrinsicSize(SvgLengthUnits.Points);
  77. if (s.Width > 0 && s.Height > 0)
  78. {
  79. // If image proportions are different from our target rectangle,
  80. // we resize the rectangle centering the image in it:
  81. var qSrc = s.Width / s.Height;
  82. var qTgt = rect.Width / rect.Height;
  83. if (qSrc < qTgt)
  84. rect.Inflate(rect.Width * (qSrc / qTgt - 1) / 2, 0);
  85. else if (qSrc > qTgt)
  86. rect.Inflate(0, rect.Height * (qTgt / qSrc - 1) / 2);
  87. }
  88. // Render the SVG:
  89. g.DrawSvg(svg, rect);
  90. // Move on:
  91. ip.X += sWidth;
  92. if (ip.X + sWidth > pixelSize.Width && i < images.Count() - 1)
  93. {
  94. ip.X = margin;
  95. ip.Y += sHeight;
  96. }
  97. }
  98. // Dispose images after saving the result:
  99. images.ForEach(t_ => t_.Item2.Dispose());
  100. // Done:
  101. return bmp;
  102. }
  103. }
  104. }
  105.