Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
矢量化是将光栅图像转换为矢量图形的过程,矢量图形使用贝塞尔曲线、样条曲线和线条等元素。这一过程至少包括颜色量化、检测单色图形的边界、用贝塞尔曲线或多边形对其进行逼近,以及通过降噪改善路径质量。结果以 SVG 文件等矢量图像格式保存。
Aspose.SVG for .NET 提供了两种功能强大的方法,可通过矢量化将光栅图像转换为 SVG 格式:
本文介绍使用 Converter.ConvertImageToSVG() 方法将JPG、PNG、BMP、TIFF、GIF和 ICO 等光栅图像转换为 SVG 的基本概念和实际示例。
要进一步了解如何使用 ImageVectorization API 将图像转换为矢量图形,请参阅 图像和文本矢量化 章节。您将看到图像矢量化过程的描述、矢量化算法和参数的使用,以及使用 Vectorize() 方法的 C# 示例。
将光栅图像转换为 SVG 可实现可缩放、与分辨率无关的图形,这些图形在任何尺寸下都能保持清晰,是网页设计和其他应用的理想选择。下面的示例演示了如何使用 “ConvertImageToSVG() “方法将光栅 PNG 图像转换为 SVG:
configuration, imageFile, outputFile) 方法对输入图像进行矢量化,将其转换为可缩放的 SVG 格式并保存到指定的输出路径。添加以下命名空间
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Converters;
4using Aspose.Svg.ImageVectorization;C# code for image to SVG conversion:
1// Convert Image to SVG using C#
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);图中显示了图像矢量化的结果:a) 原始 PNG 图像;b) 生成的 SVG:

要获得最佳效果,可以尝试使用不同的配置选项。例如,较低的颜色限制和较高的平滑度可以创建更加抽象和风格化的图像,而较高的精确度设置则可以保留更多细节,但会牺牲复杂性。
ImageVectorizerConfiguration 类的一些属性:
ColorsLimit – 设置用于量化图像的最大颜色数。降低其值可简化输出并减小文件大小。LineWidth – 控制跟踪边缘的视觉粗细。BezierPathBuilder 类的属性:
TraceSmoother – 调整曲线和直线的平滑程度。ErrorThreshold 和 MaxIterations – 这些参数会影响路径精度和性能。Aspose.SVG for .NET既支持基于文件的工作流,也支持基于流的工作流,因此可灵活地用于桌面、网络和面向服务的应用程序。下面的示例展示了如何应用
ConvertImageToSVG(configuration, inputStream, outputFile) 方法将 JPG 转换为 SVG:
1// Convert raster image JPG to vector SVG using 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.");图中显示了图像矢量化的结果:a) 原始 JPG 图像;b) 生成的 SVG:

ConvertImageToSVG() 方法可通过自定义设置快速将图像转换为 SVG。另见
Aspose.SVG 提供免费的在线 图像到 SVG 转换器,可在任何平台上运行,并提供准确高效的转换。使用此应用程序,您可以应用一系列选项来实现完美的结果。节省您的时间,使用这款免费的图像到 SVG 转换器,获取矢量图形的所有好处!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.