RenderSvgContent.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 shows how to measure and render the actual SVG content
  22. // that sometimes is offset within the SVG viewport.
  23. // In the left part of the page, we render such an SVG as it is supposed
  24. // to be laid out within its viewport. A gray border is drawn around the viewport,
  25. // and a magenta border is around the SVG content.
  26. // In the right part of the page we locate just the content at a specific point
  27. // in the final image, and draw a purple border around it.
  28. //
  29. // This short sample shows how to measure and render an SVG image.
  30. // The SVG art used in this sample is from freesvg.org.
  31. public class RenderSvgContent
  32. {
  33. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  34. {
  35. // Load the SVG:
  36. var svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg");
  37. using var svg = GcSvgDocument.FromFile(svgPath);
  38.  
  39. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  40. using var g = bmp.CreateGraphics(Color.White);
  41. var pt0 = new PointF(g.Resolution, g.Resolution);
  42.  
  43. // Render the SVG content as specified by the SVG layout within viewport:
  44. var rcViewport = new RectangleF(pt0, svg.GetIntrinsicSize(SvgLengthUnits.Pixels));
  45. // Measure the content size/location relative to 'pt':
  46. var rcContent0 = g.MeasureSvg(svg, pt0);
  47. // Draw the SVG and viewport and content bounds:
  48. g.DrawSvg(svg, pt0);
  49. g.DrawRectangle(rcViewport, Color.DarkGray);
  50. g.DrawRectangle(rcContent0, Color.Magenta);
  51.  
  52. // In the left part of the bitmap, render just the SVG content
  53. // aligning its top left corner to a specific point on the bitmap
  54. // (center of page horizontally, 1" from the top):
  55. var pt1 = new PointF(pixelSize.Width / 2, g.Resolution);
  56. // An alternative to GcGraphics.MeasureSvg() is GcSvgDocument.Measure(),
  57. // here we use it for illustration but these methods are calculations heavy,
  58. // so in a real app we would re-use the results from GcGraphics.MeasureSvg() above:
  59. var rcContent1 = svg.Measure(PointF.Empty, dpi);
  60. g.DrawSvg(svg, new PointF(pt1.X - rcContent1.X, pt1.Y - rcContent1.Y));
  61. // Add a border:
  62. g.DrawRectangle(new RectangleF(pt1, rcContent1.Size), Color.Purple);
  63.  
  64. // Done:
  65. return bmp;
  66. }
  67. }
  68. }
  69.