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,29 +537,33 @@ namespace Svg
/// <summary>
/// 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>
/// <returns>A <see cref="Bitmap"/> containing the rendered document.</returns>
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;
}
/// <summary>
......
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