Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Aspose.SVG 提供免费的在线 图像矢量化器,它基于浏览器,可在任何平台上运行。使用此应用程序,您可以应用一系列选项来获得完美的结果。节省您的时间并尝试这个免费的图像矢量化器,享受矢量图形的所有优势!
图像矢量化是将 PNG、JPG、BMP、GIF 或 TIFF 等光栅图像转换为可缩放的 SVG 矢量图形的过程。与位图图像不同,SVG 文件在任何分辨率下都能保持质量,并且更易于针对 Web、印刷、CAD 和图形设计工作流进行编辑、优化和缩放。
使用 Aspose.SVG for .NET,您可以在 C# 中以编程方式矢量化光栅图像,无需外部图形软件或基于浏览器的工具。该 API 提供灵活的矢量化设置,允许您控制路径平滑、曲线简化、颜色减少和 SVG 输出复杂度。
本文演示了如何:
注意: Aspose.Svg.ImageVectorization 命名空间支持常见的位图格式(JPG、PNG、BMP、TIFF、GIF),并且始终生成 SVG 文件。
Aspose.SVG 图像矢量化 API 提供的类和方法允许您在保存为矢量格式之前使用各种图像预处理选项。矢量化过程可以控制以下选项:TraceSimplifier、TraceSmoother、ColorsLimit 和 LineWidth。
让我们看看 TraceSimplifier 属性如何影响图像矢量化。首先,您需要了解:
tolerance) 构造函数将 tolerance(容差)作为参数并初始化该类的实例。tolerance 值确定从轨迹中消除点所允许的最大误差容限。它必须在 0 到 4 的范围内。默认值为 0.3。以下代码片段演示了使用不同 tolerance (TraceSimplifier) 属性值进行图像到矢量的转换:
1using System.IO;
2using Aspose.Svg.ImageVectorization;
3using Aspose.Svg.Saving; 1// Compare different SplinePathBuilder configurations for image vectorization with varying simplification levels
2
3// Initialize an instance of the ImageVectorizer class and specify configuration properties
4ImageVectorizer vectorizer1 = new ImageVectorizer
5{
6 Configuration =
7 {
8 PathBuilder = new SplinePathBuilder
9 {
10 TraceSimplifier = new ImageTraceSimplifier(0.1f),
11 TraceSmoother = new ImageTraceSmoother(2),
12 },
13 ColorsLimit = 2
14 }
15};
16
17ImageVectorizer vectorizer2 = new ImageVectorizer
18{
19 Configuration =
20 {
21 PathBuilder = new SplinePathBuilder
22 {
23 TraceSimplifier = new ImageTraceSimplifier(1),
24 TraceSmoother = new ImageTraceSmoother(2),
25 },
26 ColorsLimit = 2
27 }
28};
29
30ImageVectorizer vectorizer3 = new ImageVectorizer
31{
32 Configuration =
33 {
34 PathBuilder = new SplinePathBuilder
35 {
36 TraceSimplifier = new ImageTraceSimplifier(2),
37 TraceSmoother = new ImageTraceSmoother(2),
38 },
39 ColorsLimit = 2
40 }
41};
42
43// Prepare a path for a source image file
44string sourcePath = Path.Combine(DataDir, "formats.png");
45
46// Vectorize raster image from the specified file
47using SVGDocument document1 = vectorizer1.Vectorize(sourcePath);
48using SVGDocument document2 = vectorizer2.Vectorize(sourcePath);
49using SVGDocument document3 = vectorizer3.Vectorize(sourcePath);
50
51// Save the vectorized image as an SVG file
52document1.Save(Path.Combine(OutputDir, "formats1.svg"));
53document2.Save(Path.Combine(OutputDir, "formats2.svg"));
54document3.Save(Path.Combine(OutputDir, "formats3.svg"));
有时轮廓片段看起来像锯齿波或显得参差不齐。让我们看看 TraceSmoother 属性如何影响平滑:
severity) 构造函数将 severity(强度)作为参数。severity 的值决定了查询点考虑的区域范围。它必须在 0 到 20 的范围内。让我们看看 TraceSmoother 属性如何影响矢量化结果:
1using System.IO;
2using Aspose.Svg.ImageVectorization;
3using Aspose.Svg.Saving; 1// Vectorize PNG to SVG using ImageVectorizer and custom parameters
2
3// Initialize an instance of the ImageVectorizer class
4ImageVectorizer vectorizer = new ImageVectorizer
5{
6 // Optionally set a configuration
7 Configuration =
8 {
9 // Optionally create an instance of the PathBuilder class
10 PathBuilder = new BezierPathBuilder {
11 // Optionally set trace smoother
12 TraceSmoother = new ImageTraceSmoother(0),
13 ErrorThreshold = 30,
14 MaxIterations = 30
15 },
16 ColorsLimit = 10,
17 LineWidth = 1
18 }
19};
20// Vectorize image from the specified file
21using SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "flower.png"));
22
23// Save vectorized Image as an SVG file
24document.Save(Path.Combine(OutputDir, "flower.svg"));
是否可以将照片转换为矢量格式并使其看起来与原图完全一致?
SVG 并不太适合照片级逼真的图像,因为它目前还不支持完美的自然颜色过渡。矢量图形最适合用于徽标、图标和技术图纸。然而,矢量化照片可以产生令人印象深刻的艺术效果。
在本节中,我们尝试选择矢量化选项,使结果尽可能接近照片:
1using System.IO;
2using Aspose.Svg.ImageVectorization;
3using Aspose.Svg.Saving; 1// Convert JPG to SVG in C#
2
3// Initialize an instance of the ImageVectorizer class
4ImageVectorizer vectorizer = new ImageVectorizer
5{
6 // Optionally set a configuration
7 Configuration =
8 {
9 // Optionally create an instance of the PathBuilder class
10 PathBuilder = new SplinePathBuilder
11 {
12 TraceSmoother = new ImageTraceSmoother(1),
13 TraceSimplifier = new ImageTraceSimplifier(0.3f)
14 },
15 ColorsLimit = 25,
16 LineWidth = 1
17 }
18};
19
20// Vectorize image from a specified file
21using (SVGDocument document = vectorizer.Vectorize(Path.Combine(DataDir, "horses.jpg")))
22{
23 // Save the vectorized image as SVG
24 document.Save(Path.Combine(OutputDir, "horses-new.svg"));
25}该图展示了源照片 (a)、使用代码矢量化的图像 (b) 以及手动改进的矢量化图像 (c)。

矢量化过程使用颜色量化。小的同色区域会被几何形状替换,这可能会在它们之间留下微小的间隙。这就是矢量化图像中出现白点的原因。
提示: 要解决此问题,您可以手动编辑生成的 SVG 文件。我们建议将 stroke-width="100" 值更改为 "150" 或其他合适的值。

源照片 (a) 和生成的 SVG 文件 (c) 的详细信息可以通过以下链接查看: horses.jpg、 horses.svg。
Aspose.SVG for .NET 提供多种选项来控制 SVG 质量、路径平滑度和输出复杂度。
| 选项 | 描述 | 推荐用法 |
|---|---|---|
ColorsLimit | 定义最大颜色数。较低的值会简化结构,较高的值会保留细节。 | 徽标使用较低值;插图和照片使用较高值。 |
ImageSizeLimit | 定义最大图像尺寸(宽 × 高)。大图像可能会自动缩放。 | 对于高分辨率源图像,请增加此值。 |
PathBuilder | 指定路径生成算法。影响曲线的平滑度。 | 使用 SplinePathBuilder 获得更平滑的路径。 |
TraceSimplifier | 减少路径点数并简化几何结构。 | 有助于减小文件大小并优化路径复杂度。 |
TraceSmoother | 平滑轮廓并减少矢量路径中的锯齿边缘。 | 推荐用于有噪点或低质量的光栅图像。 |
LineWidth | 定义生成的矢量描边的宽度。 | 如果细轮廓在矢量化过程中消失,请增加该值。 |
Aspose.SVG 的免费评估版提供了几乎所有功能,但有以下限制:
要不受限制地尝试 Aspose.SVG,请申请 30 天临时许可证。

生成的文件: horses-license.svg。
| 问题 | 可能的原因 | 推荐的解决方案 |
|---|---|---|
| SVG 包含许多微小间隙 | ColorsLimit 设置过低。 | 增加 ColorsLimit(例如到 30–40)或增加 stroke-width。 |
| 生成的 SVG 文件太大 | TraceSimplifier 容差过低。 | 增加容差值(通常为 0.7–1.0)以减少路径点。 |
| 边缘看起来粗糙或有锯齿 | TraceSmoother 已禁用或设置过低。 | 使用 5–10 之间的平滑值。 |
| 输出 SVG 中缺少某些颜色 | 评估版限制了处理的主颜色数量。 | 申请临时或正式 Aspose.SVG 许可,以启用全彩矢量化支持。 |
| SVG 包含过多不必要的路径 | 源图像包含噪点、渐变或压缩伪影。 | 在矢量化前对图像进行预处理,例如减少噪点、增加对比度或使用更简单的源图形。 |
| 照片矢量化结果不准确 | 光栅到矢量的追踪最适合徽标、图标和插图,而非细节丰富的照片。 | 减少颜色数量、增加平滑度,或使用简化作品代替写实照片。 |
| 细线消失 | 简化或颜色减少过于激进。 | 降低容差并增加 ColorsLimit。 |
1. 如何在 C# 中将 PNG 转换为 SVG?
使用 Aspose.SVG for .NET 中的 ImageVectorizer 类。该 API 支持平滑、简化和颜色减少的配置。
2. 光栅图像和矢量图形有什么区别?
光栅图像由像素组成,缩放时会失真。矢量图形使用数学路径,可以无限缩放而不损失质量。
3. 如何在矢量化后减小 SVG 文件大小?
增加简化容差,减少颜色数量,并使用路径平滑设置。
4. 哪些图像最适合矢量化?
边缘清晰、颜色有限的徽标、图标和插图通常能获得最佳效果。
5. 我可以自定义矢量化过程吗?
Yes. Aspose.SVG for .NET allows you to configure color limits, path builders, smoothing, simplification, and image size settings to control SVG output quality.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.