SVG Rendering

To render an Leadtools.Svg.SvgDocument to a target device:

  1. Obtain an instance of IRenderingEngine

  2. Set up the rendering options using SvgRenderOptions

  3. Call SvgDocument.Render

An IRenderingEngine can be obtained using one of the following methods:

SvgRenderOptions contains the following properties:

Member Description
Bounds

The destination rectangle, in pixel coordinates. The SVG document will be rendered to fit and center inside this rectangle. SvgRenderOptions.Transform will be applied to this rectangle.

ClipBounds

The clipping rectangle, in pixel coordinates. The engine will apply this clipping into Bounds to exclude all or parts of the objects that do not fit. The transformation set in SvgRenderOptions.Transform will not be applied to this rectangle.

Transform

The transformation to be applied. This is a LeadMatrix instance that you can use for zooming, translation and rotation of the final document.

BackgroundColor

SVG documents do not have a background color. Use this property to fill Bounds (final transformation applied) with a color. UseBackgroundColor must be true.

UseBackgroundColor

Use BackgroundColor to fill the background of the transformed destination rectangle.

Important Note: SvgDocument.Render will throw an exception if the SvgDocument is not flat or does not contain valid bounds. For more information, refer SVG Size, Bounds and Flat.

The following code illustrates how to render a document by automatically checking its flat and bounds state:

void RenderDocument(IRenderingEngine engine, SvgRenderOptions options, SvgDocument svgDocument) 
{ 
   // If the document is not flat, flatten it and automatically calculate its size 
   if (!svgDocument.IsFlat) 
      svgDocument.Flat(null); 
             
   // If the document does not have a valid bounds, calculate it now automatically 
   var svgBounds = svgDocument.Bounds; 
   if (!svgBounds.IsValid) 
      svgDocument.CalculateBounds(false); 
             
   // Now, render the document 
   svgDocument.Render(engine, options); 
} 

Leadtools.Svg.SvgDocument will create internal data structures to speed up rendering. This data can be cached and re-used by using SvgDocument.BeginRenderOptimize and SvgDocument.EndRenderOptimize.

If the Leadtools.Svg.SvgDocument will be rendered once, it is best to leave the optimization as is. However, if the document will be rendered many times (for example, it is being viewed in an ImageViewer control, then it is best to cache this data to enhance speed. This can easily be achieved by using BeginRenderOptimize before setting the document in the viewer.

BeginRenderOptimize may use extra system resources stored in the internal data. The EndRenderOptimize can be used to manually free this data at any time.

BeginRenderOptimize and EndRenderOptimize use an internal counter to track the state of the optimization. BeginRenderOptimize increments this counter and EndRenderOptimize decrements it, therefore the optimization is only performed when needed and any number of BeginRenderOptimize and EndRenderOptimize can be chained together without affecting the document.

This internal data will automatically be freed when the document is disposed, regardless of the value of the counter.

To determine if the document has optimized rendering data, use SvgDocument.IsRenderOptimized.

Related Topics

Working with SVG
SVG Size, Bounds and Flat
Help Version 23.0.2024.8.27
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.