Whenever transparency is needed in images on the web, PNG is one of the formats that come to mind. SVG image is also one of the commonly used image formats due to its scalability. For displaying small static images, logos, and for images with transparent backgrounds, PNG images are often preferred. Due to compatibility or any other reason, it is often required to convert SVG vector graphics into other formats. This article discusses how to convert SVG vector graphics to PNG images using C#.
.NET API to Convert Images
GroupDocs empowers develpers with its Conversion API to convert image formats into various other formats. The converted image can also be altered while the conversion process by using different advanced options. In this article, I will use its GroupDocs.Conversion for .NET to convert the SVG vector image into PNG images.
You can download the DLLs or MSI installer from the downloads section or install the API by adding its package to your .NET application via NuGet.
PM> Install-Package GroupDocs.Conversion
Running examples for its features are also available on GitHub. Visit its documentation and API Reference for guidance.
Convert SVG to PNG Image using C#
Let’s begin with the conversion of vector graphics into PNG format using the default settings. The following steps show how to convert the SVG image into PNG using C#.
- Load the SVG file using the Converter class.
- Prepare the image conversion options.
- Set the conversion file type as PNG.
- Invoke Convert() method with prepared options.
The following C# source code converts the SVG into a PNG image.
SVG to PNG Image using Advanced Options using C#
Various customizations can be applied to PNG output while conversion of the vector graphics. Here are some of the PNG image features that we can adjust:
- Brightness
- Contrast
- Gamma
- Grayscale
- Flip Mode (Horizontal / Vertical)
- Rotation Angles
- Height
- Width
- Watermark
The following steps show how to convert an SVG image to PNG, and further apply adjustments to the resultant PNG file.
- Load the SVG graphic using the Converter class.
- Prepare the image conversion options.
- Set the conversion file type as PNG.
- Define the selective customizations and adjust their values.
- Convert to a PNG image using the Convert() method with prepared options.
The following source code applies customization in SVG to PNG conversion using C#. You can apply any of these or the combination of effects for the desired PNG output.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
Let’s conclude. We learned the basis and little advanced conversion of SVG vector graphics into PNG format using C# within .NET applications. We further learned how to rotate, flip or add watermark to SVG files when converted to PNG format. Additionally, we learned to fine-tune the gamma and brightness, and how to ON and OFF the grayscale options.
Try running examples from GitHub that will help you build your own SVG to PNG Converter similar to the one available at groupdocs.app. Contact us for any query via the forum.