Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/5515a703ca781ec04e071f3d174678ce to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/5515a703ca781ec04e071f3d174678ce to your computer and use it in GitHub Desktop.
This Gist contains examples Aspose.SVG Image and Text Vectorization

Aspose.SVG for .NET — Image & Text Vectorization Examples

This Gist contains practical C# code examples from the Image and Text Vectorization chapter of the Aspose.SVG for .NET documentation. These examples illustrate how to convert raster images and text to vector SVG graphics using ImageVectorizer and save text elements as vector paths.

Topics Covered

  • Simple Image Vectorization – Convert raster images like PNG and JPG to SVG without any configuration using ImageVectorizer.
  • Photo and Image Vectorization – Vectorize photos and raster images with customizable path tracing and color constraints.
  • Advanced Customization – Customize PathBuilder, TraceSmoother, TraceSimplifier, line width, and color limits for precise SVG output.
  • Stencil Effects – Apply stencil effects during image raster-to-vector processing.
  • Trace Adjustments – Compare different results by changing the TraceSimplifier and TraceSmoother properties.
  • Vectorize Text Elements – Convert text in SVG documents to vector paths by enabling VectorizeText in SVGSaveOptions.

Getting Started

  1. Create a C# project and add the Aspose.SVG for .NET package via NuGet.
  2. Find the gist that corresponds to your specific task. Copy or download this gist to your local machine.
  3. Open the example and replace DataDir and OutputDir with the actual file paths.
  4. Run the example to vectorize an image or text and check the results.

You can download a free trial of Aspose.HTML for .NET and use a temporary license for unrestricted access.

Why Use Vectorization

  • High quality results: Convert pixel-based images into scalable SVG files for high-resolution, device-independent use.
  • Custom trace control: Change trace smoothing, simplification, and pathing for clean, optimized SVG output.
  • Text as vectors: Convert text into smooth vector paths. It can be scaled with saving quality, and you can apply any SVG effects to it.

About Aspose.SVG for .NET

Aspose.SVG for .NET is a powerful API designed for working with SVG files in C# applications. This cross-platform API allows developers to create, load, edit, render, convert, and merge SVG files. Additionally, in the area of image and text vectorization, the library offers tools to convert raster images (such as PNG, JPEG, BMP, etc.) into scalable SVG graphics and to transform text into vector paths for precise rendering.

Prerequisites

  • .NET Platforms: .NET Framework 4.6.1+, .NET Core 2.0+, or .NET 5.
  • Supported OS: Windows, Linux, macOS.
  • Development environment such as Visual Studio.
  • Aspose.SVG for .NET installed via NuGet.

Resources & Support

Aspose.SVG for .NET Image and Text Vectorization
// Convert JPG to SVG in C#
// Learn more: https://docs.aspose.com/svg/net/image-vectorization-examples/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
// Optionally set a configuration
Configuration =
{
// Optionally create an instance of the PathBuilder class
PathBuilder = new SplinePathBuilder
{
TraceSmoother = new ImageTraceSmoother(1),
TraceSimplifier = new ImageTraceSimplifier(0.3f),
},
ColorsLimit = 25,
LineWidth = 1
}
};
// Vectorize image from a specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "horses.jpg"));
// Save the vectorized image as SVG
document.Save(Path.Combine(OutputDir, "horses-new.svg"));
// Vectorize PNG to SVG in C# using custom parameters
// Learn more: https://docs.aspose.com/svg/net/vectorization/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
// Optionally set a configuration
Configuration =
{
PathBuilder = new SplinePathBuilder
{
TraceSimplifier = new ImageTraceSimplifier(0.1f),
TraceSmoother = new ImageTraceSmoother(1),
},
ColorsLimit = 2
}
};
// Vectorize image from a specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "flower.png"));
// Save the vectorized Image as an SVG file
document.Save(Path.Combine(OutputDir, "flower-vectorized.svg"));
// Vectorize PNG to SVG using ImageVectorizer and custom parameters
// Learn more: https://docs.aspose.com/svg/net/vectorization/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
// Optionally set a configuration
Configuration =
{
// Optionally create an instance of the PathBuilder class
PathBuilder = new BezierPathBuilder {
// Optionally set trace smoother
TraceSmoother = new ImageTraceSmoother(0),
ErrorThreshold = 30,
MaxIterations = 30
},
ColorsLimit = 10,
LineWidth = 1
}
};
// Vectorize image from the specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "flower.png"));
// Save vectorized Image as an SVG file
document.Save(Path.Combine(OutputDir, "flower.svg"));
// Compare different SplinePathBuilder configurations for image vectorization with varying simplification levels
// Learn more: https://docs.aspose.com/svg/net/image-vectorization-examples/
// Initialize an instance of the ImageVectorizer class and specify configuration properties
ImageVectorizer vectorizer1 = new ImageVectorizer
{
Configuration =
{
PathBuilder = new SplinePathBuilder
{
TraceSimplifier = new ImageTraceSimplifier(0.1f),
TraceSmoother = new ImageTraceSmoother(2),
},
ColorsLimit = 2
}
};
ImageVectorizer vectorizer2 = new ImageVectorizer
{
Configuration =
{
PathBuilder = new SplinePathBuilder
{
TraceSimplifier = new ImageTraceSimplifier(1),
TraceSmoother = new ImageTraceSmoother(2),
},
ColorsLimit = 2
}
};
ImageVectorizer vectorizer3 = new ImageVectorizer
{
Configuration =
{
PathBuilder = new SplinePathBuilder
{
TraceSimplifier = new ImageTraceSimplifier(2),
TraceSmoother = new ImageTraceSmoother(2),
},
ColorsLimit = 2
}
};
// Prepare a path for a source image file
string sourcePath = Path.Combine(DataDir, "formats.png");
// Vectorize raster image from the specified file
using SVGDocument document1 = vectorizer1.Vectorize(sourcePath);
using SVGDocument document2 = vectorizer2.Vectorize(sourcePath);
using SVGDocument document3 = vectorizer3.Vectorize(sourcePath);
// Save the vectorized image as an SVG file
document1.Save(Path.Combine(OutputDir, "formats1.svg"));
document2.Save(Path.Combine(OutputDir, "formats2.svg"));
document3.Save(Path.Combine(OutputDir, "formats3.svg"));
// Convert PNG to SVG using mono-color stencil effect
// Learn more: https://docs.aspose.com/svg/net/image-stencil/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
// Optionally set a configuration
Configuration =
{
// Optionally create an instance of the PathBuilder class
PathBuilder = new BezierPathBuilder {
// Optionally set trace smoother
TraceSmoother = new ImageTraceSmoother(1),
ErrorThreshold = 30,
MaxIterations = 30
},
ColorsLimit = 10,
LineWidth = 1,
// Set stencil effect configuration
Stencil = new StencilConfiguration { Type = StencilType.MonoColor, Color = Aspose.Svg.Drawing.Color.FromRgb(0,0,255) }
}
};
// Vectorize image from a specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "flower.png"));
// Save the vectorized image as an SVG file
document.Save(Path.Combine(OutputDir, "flower.svg"));
// Vectorize PNG into SVG using ImageVectorizer with custom smoothing and simplification
// Learn more: https://docs.aspose.com/svg/net/vectorization/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
Configuration =
{
PathBuilder = new SplinePathBuilder
{
TraceSmoother = new ImageTraceSmoother(2),
TraceSimplifier = new ImageTraceSimplifier(0.1f),
},
ColorsLimit = 2
}
};
// Vectorize raster image from a specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "png-to-svg.png"));
// Save the vectorized image as SVG file
document.Save(Path.Combine(OutputDir, "png-to-svg.svg"));
// Vectorize JPG to SVG in C#
// Learn more: https://docs.aspose.com/svg/net/image-vectorization-examples/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer
{
// Optionally set a configuration
Configuration =
{
// Optionally create an instance of the PathBuilder class
PathBuilder = new BezierPathBuilder {
//optionally set trace smoother
TraceSmoother = new ImageTraceSmoother(1),
ErrorThreshold = 30,
MaxIterations = 30
},
ColorsLimit = 25,
LineWidth = 1
}
};
// Vectorize image from a specified file
using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "horses.jpg"));
// Save the vectorized image as an SVG file
document.Save(Path.Combine(OutputDir, "horses-vectorized.svg"));
// Vectorize PNG to SVG in C#
// Learn more: https://docs.aspose.com/svg/net/vectorization/
// Initialize an instance of the ImageVectorizer class
ImageVectorizer vectorizer = new ImageVectorizer();
// Vectorize image from a specified file
SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "flower.png"));
// Save the vectorized image as an SVG file
document.Save(Path.Combine(OutputDir, "flower.svg"));
// Vectorize text elements in an SVG document using C#
// Learn more: https://docs.aspose.com/svg/net/text-vectorization/
// Load an SVG document from file
SVGDocument document = new SVGDocument(Path.Combine(DataDir, "tree.svg"));
// Set text elements vectorization
SVGSaveOptions saveOptions = new SVGSaveOptions
{
VectorizeText = true
};
// Save the SVG document with specified saveOptions
document.Save(Path.Combine(OutputDir, "text_vectorized.svg"), saveOptions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment