Click or drag to resize
AB4D logo

ReaderSvgRenderToBitmap Method (Int32, Int32, Int32, Brush)

Renders the read svg objects into bitmap with custom width and height and at dpi specified with parameter to the method.

Namespace:  Ab2d
Assembly:  Ab2d.ReaderSvg (in Ab2d.ReaderSvg.dll) Version: 7.1.7295.1040
Syntax
C#
public BitmapSource RenderToBitmap(
	int customWidth,
	int customHeight,
	int dpi,
	Brush backgroundBrush
)

Parameters

customWidth
Type: SystemInt32
width of the rendered bitmap (use 0 to preserve the existing width or preserve the aspect ratio when setting only height)
customHeight
Type: SystemInt32
height of the rendered bitmap (use 0 to preserve the existing height or preserve the aspect ratio when setting only width)
dpi
Type: SystemInt32
dpi used to render the bitmap
backgroundBrush
Type: System.Windows.MediaBrush
background brush

Return Value

Type: BitmapSource
BitmapSource
Examples

The following example uses the RenderToBitmap(Brush) method to render the read svg file to wpfBitmap:

var readerSvg = new Ab2d.ReaderSvg();
readerSvg.Read(@"c:\mySample.svg");

// Render to bitmap
var wpfBitmap = readerSvg.RenderToBitmap(Brushes.White);

The next example uses this method to render the read svg file to image with custom size and saves it to disk:

var readerSvg = new Ab2d.ReaderSvg();
readerSvg.Read(@"c:\mySample.svg");

// Render to bitmap with 1024 width - height will be set automatically so that the
// Image aspect ratio is preserved
var wpfBitmap = readerSvg.RenderToBitmap(1024, 0, Brushes.White);

var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
// Uncomment to save to jpg
//var encoder = new JpegBitmapEncoder();
//encoder.QualityLevel = 85;

encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(wpfBitmap));

using (var stream = System.IO.File.OpenWrite("c:\\svgImage.png"))
{
    encoder.Save(stream);
}
See Also