←Select platform

Rasterize Method

Summary
Renders this SvgDocument to the surface of a LEADTOOLS Leadtools.RasterImage object.
Syntax
C#
VB
Objective-C
C++
Java
public void Rasterize( 
   RasterImage image, 
   SvgRenderOptions options 
) 
Public Sub Rasterize( _ 
   ByVal image As RasterImage, _ 
   ByVal options As SvgRenderOptions _ 
)  
- (BOOL)rasterizeToImage:(LTRasterImage *)image 
                 options:(nullable LTSvgRenderOptions *)options 
                   erorr:(NSError **)error 
public void rasterize(RasterImage image, SvgRenderOptions options) 
public: 
void Rasterize(  
   RasterImage^ image, 
   SvgRenderOptions^ options 
)  

Parameters

image
The target image. This must be a valid Leadtools.RasterImage object that is allocated and ready to use. Cannot be null.

options
Options to use during rendering. Can be null.

Remarks

For more information, refer to SVG Rendering.

Use Render to render this SvgDocument to an output engine.

This method will throw an exception if this document is not flat (the value of IsFlat is false) or if it does not have valid physical (pixel) bounds (the value of Bounds.IsValid is false). image must be previously allocated prior to calling this method. The rendering options passed will determine the position, size and any other transformations that will occur. With these options, it is possible to render the SVG document into any destination rectangle on the image or to fill the entire image area.

If the value of options is null, then the following default options will be used:

Member Value
SvgRenderOptions.Bounds

The current physical bounds of document (Bounds.Bounds)

SvgRenderOptions.ClipBounds

Empty rectangle for rendering the document

SvgRenderOptions.Transform

Identity Leadtools.LeadMatrix

SvgRenderOptions.UseBackgroundColor

true

BackgroundColor

White color

Example

This example will create a raster image then render the pages of a PDF file into the surface of the image before saving it as a PNG file.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
private static void SvgDocumentRasterizeExample() 
{ 
   // The source PDF file 
   var sourceFile = @"C:\LEADTOOLS21\Resources\Images\Leadtools.pdf"; 
 
   using (var rasterCodecs = new RasterCodecs()) 
   { 
      // Use 300 DPI when loading document images 
      rasterCodecs.Options.RasterizeDocument.Load.Resolution = 300; 
 
      // Get the size and number of pages in the document 
      int pagePixelWidth; 
      int pagePixelHeight; 
      int pageCount; 
 
      using (var codecsImageInfo = rasterCodecs.GetInformation(sourceFile, true)) 
      { 
         pagePixelWidth = codecsImageInfo.Width; 
         pagePixelHeight = codecsImageInfo.Height; 
         pageCount = codecsImageInfo.TotalPages; 
      } 
 
      // Create a raster image that will contain all the pages as a long stripe. We want to restrict this image size to 200 pixels wide to save memory 
      var imageWidth = 200; 
      var factor = (double)imageWidth / pagePixelWidth; 
      var imageHeight = (int)(factor * pagePixelHeight * pageCount + 0.5); 
 
      // Create the raster image 
      using (var rasterImage = RasterImage.Create(imageWidth, imageHeight, 32, 96, RasterColor.FromKnownColor(RasterKnownColor.Transparent))) 
      { 
         var renderOptions = new SvgRenderOptions(); 
         renderOptions.BackgroundColor = RasterColor.FromKnownColor(RasterKnownColor.Transparent); 
         renderOptions.ClipBounds = LeadRectD.Empty; 
         renderOptions.UseBackgroundColor = false; 
 
         for (var pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
         { 
            // Load this page as SVG 
            using (var svg = rasterCodecs.LoadSvg(sourceFile, pageNumber, null) as SvgDocument) 
            { 
               // Flat it 
               if (!svg.IsFlat) 
                  svg.Flat(null); 
 
               if (!svg.Bounds.IsValid) 
                  svg.CalculateBounds(false); 
 
               // Set the background bounds 
               renderOptions.Bounds = svg.Bounds.Bounds; 
 
               // Transform is for destination, translate to our location and scale by our factor 
               var transform = LeadMatrix.Identity; 
               transform.Translate(0, (pageNumber - 1) * pagePixelHeight); 
               transform.Scale(factor, factor); 
               renderOptions.Transform = transform; 
 
               // Render this SVG to the raster image surface at the specified location and size 
               svg.Rasterize(rasterImage, renderOptions); 
            } 
         } 
 
         // Finally, save the raster image as PNG to disk 
         var targetFile = @"C:\LEADTOOLS21\Resources\Images\Leadtools_pages_stripe.png"; 
         rasterCodecs.Save(rasterImage, targetFile, RasterImageFormat.Png, 0); 
      } 
   } 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Drawing 
Imports Leadtools.Forms.DocumentWriters 
Imports Leadtools.Svg 
 
Private Shared Sub SvgDocumentRasterizeExample() 
   ' The source PDF file 
   Dim sourceFile As String = "C:\LEADTOOLS21\Resources\Images\Leadtools.pdf" 
 
   Using rasterCodecs As New RasterCodecs() 
      ' Use 300 DPI when loading document images 
      rasterCodecs.Options.RasterizeDocument.Load.Resolution = 300 
 
      ' Get the size and number of pages in the document 
      Dim pagePixelWidth As Integer 
      Dim pagePixelHeight As Integer 
      Dim pageCount As Integer 
 
      Using codecsImageInfo As CodecsImageInfo = rasterCodecs.GetInformation(sourceFile, True) 
         pagePixelWidth = codecsImageInfo.Width 
         pagePixelHeight = codecsImageInfo.Height 
         pageCount = codecsImageInfo.TotalPages 
      End Using 
 
      ' Create a raster image that will contain all the pages as a long stripe. We want to restrict this image size to 200 pixels wide to save memory 
      Dim imageWidth As Integer = 200 
      Dim factor As Double = CType(CType(imageWidth, Double) / pagePixelWidth, Double) 
      Dim imageHeight As Integer = CType(factor * pagePixelHeight * pageCount + 0.5, Integer) 
 
      ' Create the raster image 
      Using rasterImage As RasterImage = rasterImage.Create(imageWidth, imageHeight, 32, 96, RasterColor.FromKnownColor(RasterKnownColor.Transparent)) 
         Dim renderOptions As New SvgRenderOptions() 
         renderOptions.BackgroundColor = RasterColor.FromKnownColor(RasterKnownColor.Transparent) 
         renderOptions.ClipBounds = LeadRectD.Empty 
         renderOptions.UseBackgroundColor = False 
 
         For pageNumber As Integer = 1 To pageCount 
            ' Load this page as SVG 
            Using svg As SvgDocument = CType(rasterCodecs.LoadSvg(sourceFile, pageNumber, Nothing), SvgDocument) 
               ' Flat it 
               If Not svg.IsFlat Then svg.Flat(Nothing) 
               If Not svg.Bounds.IsValid Then svg.CalculateBounds(False) 
 
               ' Source is all of the page 
               renderOptions.Bounds = svg.Bounds.Bounds 
 
               ' Transfrom is for destination, translate to our location and scale by our factor 
               Dim transform As LeadMatrix = LeadMatrix.Identity 
               transform.Translate(0, (pageNumber - 1) * pagePixelHeight) 
               transform.Scale(factor, factor) 
               renderOptions.Transform = transform 
 
               ' Render this SVG to the raster image surface at the specified location and size 
               svg.Rasterize(rasterImage, renderOptions) 
            End Using 
         Next 
 
         ' Finally, save the raster image as PNG to disk 
         Dim targetFile As String = "C:\LEADTOOLS21\Resources\Images\Leadtools_pages_stripe.png" 
         rasterCodecs.Save(rasterImage, targetFile, RasterImageFormat.Png, 0) 
      End Using 
   End Using 
End Sub 
Requirements

Target Platforms

Help Version 21.0.2021.6.30
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Svg Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.