How to convert SVG to Image using Syncfusion HtmlToPdfConverter in C#
The Syncfusion® HTML-to-PDF converter is a .NET PDF library designed for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It leverages the Blink rendering engine (used by Google Chrome) to ensure high-fidelity rendering. The converter delivers reliable and accurate results, preserving all graphics, images, text, fonts, and the original layout of the HTML document or webpage.
This guide explains how to convert SVG to an image using HtmlToPdfConverter in C#. It demonstrates a simple approach to rendering SVG content and saving it as an image file.
Steps to implement SVG to Image conversion:
- Create a New Project: Start a new Console application in .NET Core to facilitate the HTML-to-PDF conversion process.
- Install Required Packages: Add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package from Nuget.org to your project.
- Set Up Your Environment: In the
Program.cs
file, include these namespaces.
C#
using Syncfusion.Drawing;
using Syncfusion.HtmlConverter;
- Implement SVG to Image Conversion: Use the following code sample to convert an SVG file into a .jpg image.
C#
// Create an instance of the Syncfusion HtmlToPdfConverter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Read the SVG content from a file
string svg = File.ReadAllText("sample.svg");
// Convert the SVG content to an image using the converter
Image image = htmlConverter.ConvertToImage(svg, "");
// Extract the image data as a byte array
byte[] imageBytes = image.ImageData;
// Save the image data to a file in JPEG format
File.WriteAllBytes("Output.jpg", imageBytes);
A complete working sample can be downloaded from GitHub.
By executing the program, you will get the JPEG image as follows.
Conclusion
I hope you enjoyed learning on how to convert SVG to Image using Syncfusion® HtmlToPdfConverter in C#.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!