Resize svg to png with transparent background

Dear team,

I have been trying with both the aspose.svg and aspose.imagign to load and resize an existing svg to a specific size png image (16 and 32 - two sizes) and preserving the transparent background.
I have tried a few options from sample codes, but nothing has done the trick.

Can someone please provide me a small sample on how to do this?

@panayiotis

  1. If you need to get the actual dimension of the SVG document, we can offer use next sample of code:
 static RectangleF GetDocumentRect(SVGDocument document)
 {
     var rect = new RectangleF(0, 0, 0, 0);
     foreach (var item in document.RootElement.ChildNodes)
     {
         var gr = item as SVGGraphicsElement;
         if (gr != null)
         {
             var m = gr.Transform.AnimVal.Aggregate(new Matrix(),
                          (m1, m2) =>
                          {
                              m1.Multiply(new Matrix(m2.Matrix.A, m2.Matrix.B, m2.Matrix.C, m2.Matrix.D, m2.Matrix.E, m2.Matrix.F));
                              return m1;
                          });
             rect = RectangleF.Union(rect, Transform(m, gr.GetBBox()));
         }
     }
     return rect;
 }

Class SVGGraphicsElement has method GetBBox which return actual bounding box for this elements.

  1. If your purpose is the creation of a result Image, PDF, XPS document which size is equal to the size of the original SVG document then you can use the next code:
using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg")))
{
    var options = new PdfRenderingOptions()
    {
        PageSetup =
        {
            Sizing = SizingType.FitContent
        }
    };
    using (var device = new PdfDevice(options, dataDir + "smiley_out.pdf"))
    {
        document.RenderTo(device);
    }
}

More information about page sizing can be found here: SizingType Enum | Aspose.SVG for .NET API Reference

In case you still face any issues, please share your sample SVG with an expected output image. We will test the scenario in our environment and address it accordingly.

Hi Asad,

The code you provided would allow the reading of the dimensions of the SVG and export to the same size as the original to a pdf.

as per my original post what I am looking for is to export the SVG to PNG with the following operations:

  • Change the size to 32x32 and 16x16 respectively
  • make the result transparent

Do you have any sample code that would accomplish the above?

@panayiotis

Please try using the below code snippet:

using (var document = new Aspose.Svg.SVGDocument("example.svg"))
{
 // Initialize an instance of ImageSaveOptions class and set BackgroundColor property
 var saveOptions = new Aspose.Svg.Saving.ImageSaveOptions();
 saveOptions.BackgroundColor = System.Drawing.Color.Transparent;
 saveOptions.PageSetup.AnyPage.Size = new Svg.Drawing.Size(32, 32);

 // Convert SVG to PNG
 Aspose.Svg.Converters.Converter.ConvertSVG(document, saveOptions, Path.Combine(dataDir, "output.png"));
}

In case you face any issue, kindly share a sample SVG file with us so that we can test the scenario in our environment and address it accordingly? Please also try to share a sample output PNG for our reference.

1 Like