#include "l_bitmap.h"
L_LTSVG_API L_INT L_SvgRasterizeDocument(bitmapHandle, flatDocHandle, options)
Renders the specified SVG document to the surface of the specified LEADTOOLS BITMAPHANDLE.
The target image. This must be a valid BITMAPHANDLE that is allocated and ready to use. Cannot be NULL.
Handle to the SVG document to be rendered.
Options to use during rendering. Can be NULL.
| Value | Meaning |
|---|---|
| SUCCESS | The function was successful. |
| < 1 | An error occurred. Refer to Return Codes. |
Support for SVG is only available in the Document and Medical Imaging toolkits.
The bitmapHandle must be previously allocated prior to calling this function. The rendering options passed will determine the position, size and any other transformation 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 whole image area.
This function will return an error if the specified SVG document is not flat or if it does not have valid physical (pixel) bounds.
If the options is NULL, then these default options will be used:
| Member | Value |
|---|---|
| Bounds | The current physical bounds of document. |
| ClipBounds | Empty rectangle to render the whole document. |
| Transform | Identity. |
| UseBackgroundColor | TRUE |
| BackgroundColor | White color |
Required DLLs and Libraries
Win32, x64, Linux.
This example will render an SVG document to a BITMAPHANDLE.
L_INT SvgRasterizeDocumentExample(L_SvgNodeHandle docHandle, BITMAPHANDLE* bitmap){L_SvgNodeHandle flatDocHandle = docHandle;// If the document is non-flat, flatten it, automatically calculating the sizeL_BOOL isFlat = TRUE;L_INT nRet = L_SvgIsFlatDocument(docHandle, &isFlat);if (!isFlat)L_SvgFlatDocument(docHandle, &flatDocHandle, NULL);L_SvgBounds bounds;L_SvgGetBounds(flatDocHandle, &bounds, sizeof(L_SvgBounds));if (!bounds.IsValid)L_SvgCalculateBounds(flatDocHandle, FALSE);// Optimize it for rendering to increase the speedL_BOOL optimized = L_SvgIsRenderOptimizedDocument(flatDocHandle);if(!optimized)L_SvgBeginRenderOptimizeDocument(flatDocHandle);// This is our paint code// We will fit and center this SVG document in the BITMAPHANDLE's dimensionsL_RECTD dstBounds;dstBounds.x = dstBounds.y = 0;dstBounds.width = bitmap->Width;dstBounds.height = bitmap->Height;if (dstBounds.width < 1 || dstBounds.height < 1)return FAILURE;// Create the transformation matrixL_MATRIX transform;L_Matrix_Identity(&transform);L_SvgGetBounds(flatDocHandle, &bounds, sizeof(L_SvgBounds));L_RECTD srcBounds = bounds.Bounds;// Calculate the zoom so we can fitL_DOUBLE zoom = 1.0;if (dstBounds.width > dstBounds.height){zoom = dstBounds.width / srcBounds.width;if ((zoom * srcBounds.height) > dstBounds.height)zoom = dstBounds.height / srcBounds.height;}else{zoom = dstBounds.height / srcBounds.height;if ((zoom * srcBounds.width) > dstBounds.width)zoom = dstBounds.width / srcBounds.width;}// We have the zoom factor, set it in the transformL_Matrix_Scale(&transform, zoom, zoom);// CenterL_DOUBLE xOffset = (dstBounds.width - (zoom * srcBounds.width)) / 2.0;L_DOUBLE yOffset = (dstBounds.height - (zoom * srcBounds.height)) / 2.0;L_Matrix_Translate(&transform, xOffset, yOffset);// Now setup the rendering optionsL_SvgRenderOptions renderOptions = {0};renderOptions.StructSize = sizeof(L_SvgRenderOptions);// Use our transformrenderOptions.Transform = transform;// no clippingL_RectD_FromLTRB(&renderOptions.ClipBounds, 0, 0, dstBounds.width, dstBounds.height);// Fill the background with a white colorrenderOptions.UseBackgroundColor = TRUE;renderOptions.BackgroundColor = RGB(255,255,255);renderOptions.Bounds = srcBounds;// Rasterize the documentnRet = L_SvgRasterizeDocument(bitmap, flatDocHandle, &renderOptions);if (nRet != SUCCESS){if (!isFlat)L_SvgFreeNode(flatDocHandle);return nRet;}if (!optimized)nRet = L_SvgEndRenderOptimizeDocument(flatDocHandle);if(!isFlat)L_SvgFreeNode(flatDocHandle);return nRet;}