Save what is displayed in the viewbox

Aug 6, 2012 at 5:25 PM

Hello,

I would like to save only what is visible in the viewbox. Is this currently possible?
Aparently the save functionally looks at the width and height of the viewbox, but neglects the x and y point.

Rgds
PNV 

 

Apr 23, 2013 at 11:46 PM
Hi, have somebody figured this one out? I tried modifying the preserveAspectRatio with combinations of xMidYMid, xMinYMin, slice and meet. Also tried creating a renderer and using setClip with the viewbox values but no joy. Am I going in the right direction?
SvgDocument document = SvgDocument.Open(Server.MapPath("~/Content/svg/aaa.svg"));
document.Width = new SvgUnit(SvgUnitType.Pixel, 1600);
document.Height = new SvgUnit(SvgUnitType.Pixel, 900);

using (Bitmap bitmap = new Bitmap((int)Math.Ceiling(document.Width),(int)Math.Ceiling(document.Height))) {
//using (Bitmap bitmap = new Bitmap(1600, 900)) {
    using (var renderer = SvgRenderer.FromImage(bitmap)) {
        renderer.TextRenderingHint = TextRenderingHint.AntiAlias;
        renderer.TextContrast = 1;
        renderer.PixelOffsetMode = PixelOffsetMode.Half;
        renderer.SetClip(
            new Region(
                new Rectangle(
                    (int)Math.Ceiling(document.ViewBox.MinX),
                    (int)Math.Ceiling(document.ViewBox.MinY),
                    (int)Math.Ceiling(document.ViewBox.Width),
                    (int)Math.Ceiling(document.ViewBox.Height))));
      
        document.Draw(renderer);
        bitmap.Save(Server.MapPath("~/Content/img/aaa.png"));
    }
}
Apr 25, 2013 at 12:03 PM
Not sure if it's a bug in the library or i'm missing something when trying to draw with a custom renderer but this is how i solved it. I'll leave it here in case somebody else runs into the same problem.
I changed the PushTransforms method in SvgFragment.cs to adjust to the viewbox like this.
protected internal override void PushTransforms(SvgRenderer renderer)
{
    base.PushTransforms(renderer);

    if (!this.ViewBox.Equals(SvgViewBox.Empty))
    {
        if (this.ViewBox.MinX != 0 || this.ViewBox.MinY != 0)
        {
            renderer.TranslateTransform(-this.ViewBox.MinX, -this.ViewBox.MinY, MatrixOrder.Append);
        }

        renderer.ScaleTransform(this.Width.ToDeviceValue() / this.ViewBox.Width, this.Height.ToDeviceValue() / this.ViewBox.Height, MatrixOrder.Append);
    }
}
Then to convert from svg to png (no custom renderer):
SvgDocument document = SvgDocument.Open(Server.MapPath("~/Content/svg/aaa.svg"));
document.Width = new SvgUnit(SvgUnitType.Pixel, 1600);
document.Height = new SvgUnit(SvgUnitType.Pixel, 900);

using (Bitmap bitmap = document.Draw())
{
    bitmap.Save(Server.MapPath("~/Content/img/aaa.png"));
}
This are the properties of the svg object
<svg style="overflow: hidden; position: relative;" height="100%" version="1.1" width="100%" xmlns="http://www.w3.org/2000/svg" viewBox="148 83 314 176" preserveAspectRatio="xMidYMid slice">