diff --git a/Source/SvgDocument.cs b/Source/SvgDocument.cs index fd49fd2a164cbc19b0ab324eedee3146ac5f010d..107c5febcc13b54c7803ec13997b70935d14d080 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; } ///