←Select platform

Rasterize Method

Summary
Renders this SvgDocument to the surface of a LEADTOOLS Leadtools.RasterImage object.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void Rasterize( 
   RasterImage image, 
   SvgRenderOptions options 
) 
- (BOOL)rasterizeToImage:(LTRasterImage *)image options:(nullable LTSvgRenderOptions *)options error:(NSError **)error; 
public void rasterize(RasterImage image, SvgRenderOptions options) 
public: 
void Rasterize(  
   RasterImage^ image, 
   SvgRenderOptions^ options 
)  
def Rasterize(self,image,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#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Drawing; 
using Leadtools.Forms.DocumentWriters; 
using Leadtools.Svg; 
 
using Leadtools.Document.Writer; 
 
private static void SvgDocumentRasterizeExample() 
{ 
   // The source PDF file 
   var sourceFile = $@"{LEAD_VARS.ImagesDir}\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 = $@"{LEAD_VARS.ImagesDir}\Leadtools_pages_stripe.png"; 
         rasterCodecs.Save(rasterImage, targetFile, RasterImageFormat.Png, 0); 
      } 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.PrintStream; 
import java.nio.file.FileVisitOption; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.stream.Stream; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.document.writer.*; 
import leadtools.svg.*; 
 
 
public void svgDocumentRasterizeExample() { 
   // The source PDF file 
   String LEAD_VARS_ImagesDir = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String srcFilePath = combine(LEAD_VARS_ImagesDir, "Leadtools_pdf.svg"); 
 
   RasterCodecs codecs = new RasterCodecs(); 
   // Use 300 DPI when loading document images 
   codecs.getOptions().getRasterizeDocument().getLoad().setResolution(300); 
 
   CodecsImageInfo imageInfo = codecs.getInformation(srcFilePath, true); 
   // Get the size and number of pages in the document 
   int pagePixelWidth = imageInfo.getWidth(); 
   int pagePixelHeight = imageInfo.getHeight(); 
   int pageCount = imageInfo.getTotalPages(); 
 
   // 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 
   int imageWidth = 200; 
   double factor = (double) imageWidth / pagePixelWidth; 
   int imageHeight = (int) (factor * pagePixelHeight * pageCount + 0.5); 
 
   RasterImage rasterImage = RasterImage.create(imageWidth, imageHeight, 32, 96, 
         RasterColor.fromKnownColor(RasterKnownColor.TRANSPARENT)); 
   SvgRenderOptions renderOptions = new SvgRenderOptions(); 
   renderOptions.setBackgroundColor(RasterColor.fromKnownColor(RasterKnownColor.TRANSPARENT)); 
   renderOptions.setClipBounds(LeadRectD.getEmpty()); 
   renderOptions.setUseBackgroundColor(false); 
 
   for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) { 
      // Load this page as SVG 
      SvgDocument svgDocument = (SvgDocument) codecs.loadSvg(srcFilePath, pageNumber, null); 
      // Flat it 
      if (!svgDocument.isFlat()) 
         svgDocument.flat(null); 
      if (!svgDocument.getBounds().isValid()) 
         svgDocument.calculateBounds(false); 
 
      // Set the background bounds 
      renderOptions.setBounds(svgDocument.getBounds().getBounds()); 
 
      // Transform is for destination, translate to our location and scale by our 
      // factor 
      LeadMatrix transform = LeadMatrix.getIdentity(); 
      transform.translate(0, (pageNumber - 1) * pagePixelHeight); 
      transform.scale(factor, factor); 
      renderOptions.setTransform(transform); 
 
      // Render this SVG to the raster image surface at the specified location and 
      // size 
      svgDocument.rasterize(rasterImage, renderOptions); 
   } 
 
   // Finally, save the raster image as PNG to disk 
   String dstFilePath = combine(LEAD_VARS_ImagesDir, "Leadtools_pages_stripe.png"); 
   codecs.save(rasterImage, dstFilePath, RasterImageFormat.PNG, 0); 
   assertTrue(!rasterImage.isNull()); 
} 
Requirements

Target Platforms

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

Leadtools.Svg Assembly

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