From 42db3781e7daf149468939f23ae0838c85beca91 Mon Sep 17 00:00:00 2001 From: mrbean-bremen Date: Wed, 16 Jan 2019 20:16:55 +0100 Subject: [PATCH] Scale image before rendering it into a bitmap with defined size - fixes #405 --- Source/SvgDocument.cs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Source/SvgDocument.cs b/Source/SvgDocument.cs index fd49fd2..107c5fe 100644 --- a/Source/SvgDocument.cs +++ b/Source/SvgDocument.cs @@ -537,29 +537,33 @@ namespace Svg /// /// Renders the in given size and returns the image as a . + /// If one of rasterWidth and rasterHeight is zero, the image is scaled preserving aspect ratio, + /// otherwise the aspect ratio is ignored. /// /// A containing the rendered document. public virtual Bitmap Draw(int rasterWidth, int rasterHeight) { - var size = GetDimensions(); - RasterizeDimensions(ref size, rasterWidth, rasterHeight); + var imageSize = GetDimensions(); + var bitmapSize = imageSize; + RasterizeDimensions(ref bitmapSize, rasterWidth, rasterHeight); - if (size.Width == 0 || size.Height == 0) - return null; + if (bitmapSize.Width == 0 || bitmapSize.Height == 0) + return null; - var bitmap = new Bitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height)); - try - { - Draw(bitmap); - } - catch - { - bitmap.Dispose(); - throw; - } + var bitmap = new Bitmap((int)Math.Round(bitmapSize.Width), (int)Math.Round(bitmapSize.Height)); + try + { + var renderer = SvgRenderer.FromImage(bitmap); + renderer.ScaleTransform(bitmapSize.Width / imageSize.Width, bitmapSize.Height / imageSize.Height); + Draw(renderer); + } + catch + { + bitmap.Dispose(); + throw; + } - //Trace.TraceInformation("End Render"); - return bitmap; + return bitmap; } /// -- GitLab