Rasterize Method
Syntax
C#
VB
Objective-C
C++
Java
- (BOOL)rasterizeToImage:(LTRasterImage *)image
options:(nullable LTSvgRenderOptions *)options
erorr:(NSError **)error
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.
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.
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