Commit b96c50c3 authored by ubbn's avatar ubbn
Browse files

Add new Draw() method that can take raster size and render image in that given size

parent 2c3265fc
......@@ -490,6 +490,61 @@ namespace Svg
//Trace.TraceInformation("End Render");
}
/// <summary>
/// Renders the <see cref="SvgDocument"/> in given size and returns the image as a <see cref="Bitmap"/>.
/// </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);
if (size.Width == 0 || size.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;
}
//Trace.TraceInformation("End Render");
return bitmap;
}
/// <summary>
/// If both or one of raster height and width is not given (0), calculate that missing value from original SVG size
/// while keeping original SVG size ratio
/// </summary>
/// <param name="size"></param>
/// <param name="rasterWidth"></param>
/// <param name="rasterHeight"></param>
public virtual void RasterizeDimensions(ref SizeF size, int rasterWidth, int rasterHeight)
{
if (size == null || size.Width == 0)
return;
// Ratio of height/width of the original SVG size, to be used for scaling transformation
float ratio = size.Height / size.Width;
size.Width = rasterWidth > 0 ? (float)rasterWidth : size.Width;
size.Height = rasterHeight > 0 ? (float)rasterHeight : size.Height;
if (rasterHeight == 0 && rasterWidth > 0)
{
size.Height = (int)(rasterWidth * ratio);
}
else if (rasterHeight > 0 && rasterWidth == 0)
{
size.Width = (int)(rasterHeight / ratio);
}
}
public override void Write(XmlTextWriter writer)
{
//Save previous culture and switch to invariant for writing
......
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