Commit 42db3781 authored by mrbean-bremen's avatar mrbean-bremen Committed by mrbean-bremen
Browse files

Scale image before rendering it into a bitmap with defined size

- fixes #405
parent 2bcf5902
...@@ -537,20 +537,25 @@ namespace Svg ...@@ -537,20 +537,25 @@ namespace Svg
/// <summary> /// <summary>
/// Renders the <see cref="SvgDocument"/> in given size and returns the image as a <see cref="Bitmap"/>. /// Renders the <see cref="SvgDocument"/> in given size and returns the image as a <see cref="Bitmap"/>.
/// If one of rasterWidth and rasterHeight is zero, the image is scaled preserving aspect ratio,
/// otherwise the aspect ratio is ignored.
/// </summary> /// </summary>
/// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns> /// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns>
public virtual Bitmap Draw(int rasterWidth, int rasterHeight) public virtual Bitmap Draw(int rasterWidth, int rasterHeight)
{ {
var size = GetDimensions(); var imageSize = GetDimensions();
RasterizeDimensions(ref size, rasterWidth, rasterHeight); var bitmapSize = imageSize;
RasterizeDimensions(ref bitmapSize, rasterWidth, rasterHeight);
if (size.Width == 0 || size.Height == 0) if (bitmapSize.Width == 0 || bitmapSize.Height == 0)
return null; return null;
var bitmap = new Bitmap((int)Math.Round(size.Width), (int)Math.Round(size.Height)); var bitmap = new Bitmap((int)Math.Round(bitmapSize.Width), (int)Math.Round(bitmapSize.Height));
try try
{ {
Draw(bitmap); var renderer = SvgRenderer.FromImage(bitmap);
renderer.ScaleTransform(bitmapSize.Width / imageSize.Width, bitmapSize.Height / imageSize.Height);
Draw(renderer);
} }
catch catch
{ {
...@@ -558,7 +563,6 @@ namespace Svg ...@@ -558,7 +563,6 @@ namespace Svg
throw; throw;
} }
//Trace.TraceInformation("End Render");
return bitmap; return bitmap;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment