How to Convert SVG to PNG using C#

This quick how-to topic is about how to convert SVG to PNG using C# along with the detailed steps to configure the environment settings to run the code. In C# SVG to PNG is easily possible using a few lines of code and you can use the example in any of the .NET based supported application platforms like MS Windows, Linux or macOS.

Steps to Convert SVG to PNG using C#

  1. Using NuGet package manager, add Aspose.Imaging to convert SVG to PNG
  2. Add a reference to Aspose.Imaging namespace to use API calls in the code
  3. Load the sample SVG image using Image.Load method
  4. Instantiate SvgRasterizationOptions to set the SVG image height and width
  5. Instantiate PngOptions for the desired PNG
  6. Save the SVG converted to PNG on disk

The above steps in C# convert SVG to PNG using simple API calls and a few lines of code. It is quite an easy process where after installing API package using NuGet, we will load the source SVG file using Image.Load() method. Then using SvgRasterizationOptions class, we will set the SVG page width and height. Finally, we will pass on the SvgRasterizationOptions object to PngOptions instance for rendering and converting the SVG to PNG image.

Code to Convert SVG to PNG using C#

In order to convert SVG to PNG C# based simple API calls have been used. You can customize the output by using additional properties of SvgRasterizationOptions class that could set border, background color and scaling factors. Moreover, the PngOptions class, also gives you the option to set the compression level, filter types and resolution settings, etc.

In this tutorial, we learned that in order to Convert SVG to PNG C# based API can be effectively used. If you want to learn about the process of resizing the image using C#, refer to the article on how to resize Image in C#.

 English