Convert Image to SVG in C# – Vectorize Raster Images

Quick Answer

To convert an image to SVG in C#, create an ImageVectorizerConfiguration object and call Converter.ConvertImageToSVG(). Aspose.SVG for .NET supports raster inputs such as PNG, JPG, BMP, TIFF, GIF, and ICO and saves the traced vector result as an SVG file.

Vectorization is the process of converting raster images into vector graphics, which use Bezier curves, splines, and lines instead of pixels. The process includes color quantization, boundary detection, shape approximation, and path-quality improvements such as smoothing or simplification. The result is saved as a vector format, such as SVG.

Aspose.SVG for .NET provides two image vectorization workflows: the Converter.ConvertImageToSVG() facade for direct image-to-file conversion, and the ImageVectorization API when your code needs to work with the generated SVGDocument.

This article focuses on direct image-to-SVG conversion with Converter.ConvertImageToSVG(). For the full algorithm workflow and ImageVectorizer.Vectorize() examples, see Image and Text Vectorization and Image Vectorization Workflow.

When Image to SVG Conversion Works Best

Image vectorization works best when the source image contains clear shapes, limited colors, and visible boundaries. It is less suitable for full-color photos where preserving every detail would create a very complex SVG.

Source image typeRecommendation
Logos, icons, symbols, clip artGood fit for image to SVG conversion. These images usually contain clean shapes and limited colors.
Diagrams, maps, line art, technical drawingsGood fit when lines and regions are clear enough to trace.
Scanned drawingsUseful after preprocessing, such as cleaning noise and improving contrast.
PhotographsUsually not ideal for compact SVG. Vectorization may produce many paths and a large output file.
Low-resolution or noisy imagesImprove the source image first, or adjust smoothing and simplification options.

Convert Image to SVG

Use Converter.ConvertImageToSVG() when you want to convert an image file or stream directly to an SVG file. Use ImageVectorizer.Vectorize() when your code needs the vectorized result as an SVGDocument before saving, editing, or inspecting it. This article focuses on the direct conversion workflow.

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// Convert a raster image to SVG in C# using ConvertImageToSVG()
 2
 3// Prepare paths for a source image file and output SVG file
 4string imageFile = Path.Combine(DataDir, "flower-pink.png");
 5string outputFile = Path.Combine(OutputDir, "flower-pink.svg");
 6
 7// Create a configuration object
 8ImageVectorizerConfiguration configuration = new ImageVectorizerConfiguration
 9{
10    PathBuilder =
11                new BezierPathBuilder
12                {
13                    TraceSmoother = new ImageTraceSmoother(3),
14                    ErrorThreshold = 5,
15                    MaxIterations = 5
16                },
17    ColorsLimit = 15,
18    LineWidth = 2
19};
20
21// Convert Image to SVG
22Converter.ConvertImageToSVG(configuration, imageFile, outputFile);

The figure shows the result of image vectorization: a) original PNG image; b) resulting SVG:

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

Key Vectorization Options

Use ImageVectorizerConfiguration and path builder settings to balance visual detail, path smoothness, file size, and processing time.

GoalOption to reviewResult
Reduce SVG file sizeColorsLimit, trace simplificationFewer colors and fewer path points.
Control traced line thicknessLineWidthThicker or thinner generated vector strokes.
Smooth jagged edgesTraceSmoother in BezierPathBuilderCleaner curves for noisy or low-resolution images.
Preserve more detailErrorThreshold, MaxIterationsMore accurate paths, but potentially larger SVG output.
Create a stencil-like resultStencil configurationSimplified, high-contrast vector output.

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// Convert a raster JPG stream to vector SVG in C#
 2
 3// Prepare input and output
 4string imagePath = Path.Combine(DataDir, "tulip.jpg");
 5string outputFile = Path.Combine(OutputDir, "tulip.svg");
 6
 7// Open the image as a FileStream
 8using (FileStream inputStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
 9{
10    // Create vectorization configuration
11    ImageVectorizerConfiguration configuration = new ImageVectorizerConfiguration
12    {
13        PathBuilder = new BezierPathBuilder
14        {
15            TraceSmoother = new ImageTraceSmoother(1),
16            ErrorThreshold = 10,
17            MaxIterations = 1
18        },
19        ColorsLimit = 30,
20        LineWidth = 1
21    };
22
23    // Perform conversion from image stream to SVG
24    Converter.ConvertImageToSVG(configuration, inputStream, outputFile);
25}
26
27Console.WriteLine("Image converted to SVG successfully.");

The figure shows the result of image vectorization: a) original JPG image; b) resulting SVG:

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

Common Mistakes and Fixes

ProblemCommon causeFix
Output SVG is very largeThe source image has too many colors, noise, or photographic detailUse simpler source artwork, reduce ColorsLimit, resize the image, or apply trace simplification.
Vector edges look roughThe source image is low-resolution or noisyClean the image before conversion and increase smoothing with TraceSmoother.
Colors are too simplifiedColor quantization is too aggressiveIncrease ColorsLimit or use a cleaner source image with clearer color regions.
Text in the image is not editable SVG textImage vectorization traces pixels into shapesUse real SVG text when editable text is required. See SVG Text and Text Vectorization.

FAQ

Can I convert PNG to SVG in C#?
Yes. Use Converter.ConvertImageToSVG() for direct PNG to SVG conversion. For a PNG-focused workflow with ImageVectorizer, see Convert PNG to SVG in C#.

Can I convert an uploaded image stream to SVG?
Yes. Use the stream-based ConvertImageToSVG(configuration, inputStream, outputFile) overload when the input image comes from an upload, memory buffer, or cloud storage.

Should I vectorize photos to SVG?
Usually only when you intentionally want a stylized vector effect. Photos often create large SVG files with many paths, so ordinary raster formats are usually better for photographic content.

Does image to SVG conversion create editable text?
No. Raster image vectorization traces visible pixels into vector shapes. Text inside a source image does not become editable SVG <text> content.

How do I make the generated SVG smaller?
Use a cleaner source image, reduce the number of colors with ColorsLimit, resize very large inputs, and use simplification settings to reduce path complexity.

Related Resources

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!