public void Rasterize(
RasterImage image,
SvgRenderOptions options
)
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.
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 |
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 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";
}