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:

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:

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:

Text “The result of converting an image to SVG — the original PNG image (a) and the converted SVG (b) “

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:

Properties of the BezierPathBuilder class:

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:

Text “The result of converting an image to SVG — the original JPG image (a) and the converted SVG (b) “

Conclusions and Recommendations

  1. Use ConvertImageToSVG() methods for the fast image to SVG conversions with a custom setup.
  2. Use the following recommendations to achieve the best results and performance:
  1. Use stream-based input when processing uploaded files, in-memory images, or cloud data – this avoids temporary files and improves performance.
  2. For large datasets, build a loop to batch-convert images with error handling and logging.
  3. Always open and inspect the resulting SVGs in a browser or vector editor to verify quality.

See Also

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!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.