RenderSvgContent.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GrapeCity.Documents.Svg
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This sample shows how to measure and render the actual SVG content
'' that sometimes is offset within the SVG viewport.
'' In the left part of the page, we render such an SVG as it is supposed
'' to be laid out within its viewport. A gray border is drawn around the viewport,
'' and a magenta border is around the SVG content.
'' In the right part of the page we locate just the content at a specific point
'' in the final image, and draw a purple border around it.
''
'' This short sample shows how to measure and render an SVG image.
'' The SVG art used in this sample is from freesvg.org.
Public Class RenderSvgContent
    Public Function GenerateImage(
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        '' Load the SVG:
        Dim svgPath = Path.Combine("Resources", "SvgMisc", "Smiling-Girl-offset.svg")
        Using svg = GcSvgDocument.FromFile(svgPath)

            Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
            Using g = bmp.CreateGraphics(Color.White)
                Dim pt0 As New PointF(g.Resolution, g.Resolution)

                '' Render the SVG content as specified by the SVG layout within viewport:
                Dim rcViewport As New RectangleF(pt0, svg.GetIntrinsicSize(SvgLengthUnits.Pixels))
                '' Measure the content size/location relative to 'pt':
                Dim rcContent0 = g.MeasureSvg(svg, pt0)
                '' Draw the SVG and viewport and content bounds:
                g.DrawSvg(svg, pt0)
                g.DrawRectangle(rcViewport, Color.DarkGray)
                g.DrawRectangle(rcContent0, Color.Magenta)

                '' In the left part of the bitmap, render just the SVG content
                '' aligning its top left corner to a specific point on the bitmap
                '' (center of page horizontally, 1" from the top):
                Dim pt1 As New PointF(pixelSize.Width / 2.0F, g.Resolution)
                '' An alternative to GcGraphics.MeasureSvg() is GcSvgDocument.Measure(),
                '' here we use it for illustration but these methods are calculations heavy,
                '' so in a real app we would re-use the results from GcGraphics.MeasureSvg() above:
                Dim rcContent1 = svg.Measure(PointF.Empty, dpi)
                g.DrawSvg(svg, New PointF(pt1.X - rcContent1.X, pt1.Y - rcContent1.Y))
                '' Add a border:
                g.DrawRectangle(New RectangleF(pt1, rcContent1.Size), Color.Purple)
            End Using

            '' Done:
            Return bmp
        End Using
    End Function
End Class