Convert Image to SVG in C# – Vectorize JPG, PNG, BMP, TIFF, GIF, and ICO
Vectorization is the process of converting raster images into vector graphics, which utilize elements such as Bezier curves, splines, and lines. The process includes at least color quantization, detection of boundaries of single-color shapes, and their approximation by Bezier curves or polygons, as well as improvement of path quality through noise reduction. The result is saved in a vector image format, such as an SVG file.
Aspose.SVG for .NET provides two powerful approaches to convert raster images to SVG format via vectorization:
- The Converter class’s ConvertImageToSVG() methods are a quick and easy way to vectorize images using configuration presets.
- The ImageVectorization namespace includes classes and interfaces for implementing greater control over the vectorization process.
This article covers the basic concepts and practical examples for converting raster images such as JPG, PNG, BMP, TIFF, GIF, and ICO to SVG using the Converter.ConvertImageToSVG()
methods.
To learn more about how to convert images to vector graphics using ImageVectorization API, refer to the Image and Text Vectorization chapter. You will find a description of the image vectorization process, the use of vectorization algorithms and parameters, as well as C# examples of using the Vectorize() methods.
Convert Image to SVG
Converting raster images to SVG enables scalable, resolution-independent graphics that remain sharp at any size, making them ideal for web design and other applications. The following example demonstrates how to convert a raster PNG image to SVG using the ConvertImageToSVG()
method:
- The conversion process begins with the creation of an ImageVectorizerConfiguration object. This configuration controls how vectorization works, including the number of colors to retain, the level of smoothing applied, methods used to trace outlines and other settings.
- The
ConvertImageToSVG(
configuration, imageFile, outputFile
) method vectorizes the input image, converting it into a scalable SVG format and saving it to the specified output path.
Add the following namespaces:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Converters;
4using Aspose.Svg.ImageVectorization;
C# code for image to SVG conversion:
1// Prepare paths for a source image file and output SVG file
2string imageFile = Path.Combine(DataDir, "flower-pink.png");
3string outputFile = Path.Combine(OutputDir, "flower-pink.svg");
4
5// Create a configuration object
6ImageVectorizerConfiguration configuration = new ImageVectorizerConfiguration
7{
8 PathBuilder =
9 new BezierPathBuilder
10 {
11 TraceSmoother = new ImageTraceSmoother(3),
12 ErrorThreshold = 5,
13 MaxIterations = 5
14 },
15 ColorsLimit = 15,
16 LineWidth = 2
17};
18
19// Convert Image to SVG
20Converter.ConvertImageToSVG(configuration, imageFile, outputFile);
The figure shows the result of image vectorization: a) original PNG image; b) resulting SVG:
Vectorization options
To get optimal results, you can experiment with different configuration options. For example, a lower color limit and higher smoothing create a more abstract and stylized image, while higher accuracy settings preserve more detail at the expense of complexity.
Some properties of the ImageVectorizerConfiguration class:
ColorsLimit
– sets the maximum number of colors used to quantize the image. Decreasing its value simplifies the output and reduces the file size.LineWidth
– controls the visual thickness of the traced edges.
Properties of the BezierPathBuilder class:
TraceSmoother
– adjusts the smoothing level for curves and lines.ErrorThreshold
andMaxIterations
– these parameters affect path accuracy and performance.
Convert Image From Stream to SVG
Aspose.SVG for .NET supports both file-based and stream-based workflows, making it flexible for desktop, web, and service-oriented applications. The following example shows how to apply the
ConvertImageToSVG(configuration, inputStream, outputFile
) method for JPG to SVG conversion:
1// Prepare input and output
2string imagePath = Path.Combine(DataDir, "tulip.jpg");
3string outputFile = Path.Combine(OutputDir, "tulip.svg");
4
5// Open the image as a FileStream
6using (FileStream inputStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
7{
8 // Create vectorization configuration
9 ImageVectorizerConfiguration configuration = new ImageVectorizerConfiguration
10 {
11 PathBuilder = new BezierPathBuilder
12 {
13 TraceSmoother = new ImageTraceSmoother(1),
14 ErrorThreshold = 10,
15 MaxIterations = 1
16 },
17 ColorsLimit = 30,
18 LineWidth = 1
19 };
20
21 // Perform conversion from image stream to SVG
22 Converter.ConvertImageToSVG(configuration, inputStream, outputFile);
23}
24
25Console.WriteLine("Image converted to SVG successfully.");
The figure shows the result of image vectorization: a) original JPG image; b) resulting SVG:
Conclusions and Recommendations
- Use
ConvertImageToSVG()
methods for the fast image to SVG conversions with a custom setup. - Use the following recommendations to achieve the best results and performance:
- Optimize source images before conversion: use contrast-colored images to help the converter detect clean boundaries; resize large images to speed up processing and improve quality.
- Choose the proper ImageVectorizerConfiguration settings. Even with the default values, the conversion produces a quality SVG output. However, fine-tuning these parameters is often necessary to get the desired balance between accuracy and simplicity.
- Use stream-based input when processing uploaded files, in-memory images, or cloud data – this avoids temporary files and improves performance.
- For large datasets, build a loop to batch-convert images with error handling and logging.
- Always open and inspect the resulting SVGs in a browser or vector editor to verify quality.
See Also
- If you are interested in learning how to create vector stencils from raster images using C#, refer to the article Image Stencil – Create Stencil Image in C#.
- You can use the web-based application Stencil Drawing to experience creating vector stencils from images.
- In the article Image Vectorization Workflow, you will learn how the image vectorization process works, how to apply vectorization algorithms and configuration options, and how to convert raster images like PNG, JPG, BMP, TIFF, GIF, and ICO into scalable SVG documents using ImageVectorizer class.
Aspose.SVG offers a Free Online Image to SVG Converter that works on any platform and provides accurate and efficient conversion. Using this application, you can apply a set of options to achieve the perfect result. Save your time and check this free Image to SVG Converter to get all the benefits of vector graphics!