Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
SVG paths 是创建自定义矢量图形、曲线、轮廓和复杂形状的强大 SVG 功能之一。使用 Aspose.SVG Builder API,开发人员可以在 C# 中以可读的 fluent syntax 以编程方式创建 SVG path elements,而无需手动编写 SVG path data 字符串。
本文介绍如何在 C# 中创建 SVG paths、以编程方式构建复杂矢量图形,以及使用 Aspose.SVG for .NET 创建 SVG text paths。
PathBuilder 类通过常见 SVG path commands 的方法简化 SVG paths 创建,例如 M、L、C、Q、A 和 Z。
Aspose.SVG Builder API 提供
SVGPathElementBuilder 类,用于以编程方式构造 SVG <path> 元素。使用 PathBuilder,开发人员可以通过 fluent chained methods 定义 SVG path commands。
SVG paths 常用于:
SVG Builder API 使用嵌套 builders 来直观地定义复杂 SVG 结构。在下面的代码片段中,PathBuilder 是 builder within a builder 的示例:
SVGSVGElementBuilder 创建 <svg> 元素,而 PathBuilder 构建内部 <path> 元素。
1using Aspose.Svg.Builder;
2using System.Drawing;
3using System.IO; 1// Create an SVG path element with line commands programmatically in C# using SVG Path Builder
2
3// Initialize an SVG document
4using (SVGDocument document = new SVGDocument())
5{
6 // Create an <svg> element with specified width, height and viewBox, and add a <path> element to it
7 SVGSVGElement svg = new SVGSVGElementBuilder()
8 .Width(200).Height(200)
9 .ViewBox(0, 0, 150, 150)
10
11 .AddPath(p => p
12 // 'D' method defines the 'd' attribute, which contains the path data
13 .D(d => d
14 // 'M' sets the starting point of the path (Move to x=50, y=50)
15 .M(50, 50)
16 // 'L' draws a line from the current point to the new point (Line to x=100, y=50)
17 .L(100, 50)
18 // Another 'L' to draw a line to a new point (Line to x=100, y=100)
19 .L(100, 100)
20 // 'Z' closes the path by drawing a line to the starting point
21 .Z())
22 // Sets the fill color of the path to teal
23 .Fill(Color.Teal))
24 .Build(document.FirstChild as SVGSVGElement);
25
26 // Save the SVG document
27 document.Save(Path.Combine(OutputDir, "path.svg"));
28}在此示例中,
AddPath() 定义 <path> 元素。
D() 方法使用 SVG path commands 定义 SVG path data:
Fluent syntax 简化 SVG path 生成,并使 path 定义更易读和维护。
PathBuilder 支持绝对和相对 SVG path commands,用于以编程方式创建线条、曲线、弧线和复杂矢量图形。
| Command | Relative Command | 描述 |
|---|---|---|
M | m | 移动当前点 |
L | l | 绘制直线 |
H | h | 绘制水平线 |
V | v | 绘制垂直线 |
C | c | 绘制三次 Bézier 曲线 |
S | s | 绘制平滑三次 Bézier 曲线 |
Q | q | 绘制二次 Bézier 曲线 |
T | t | 绘制平滑二次 Bézier 曲线 |
A | a | 绘制椭圆弧 |
Z | z | 关闭当前 path |
Path data 也可以使用 AddPathSegment() 方法直接以字符串形式添加。
1.D(d => d.AddPathSegment(
2 "M199 89 C206 66 235 25 270 30 286 38 298 59 310 73"
3))当导入现有 SVG path data 或使用外部生成的 SVG 图形时,此方法很有用。
SVG paths 支持二次和三次 Bézier curves,可用于创建平滑矢量图形和复杂轮廓。
1using Aspose.Svg.Builder;
2using System.Drawing;
3using System.IO;
4
5using (SVGDocument document = new SVGDocument())
6{
7 // Create an <svg> element with specified width, height and viewBox, and add a <path> element to it
8 SVGSVGElement svg = new SVGSVGElementBuilder()
9 .Width(500).Height(500)
10 .ViewBox(0, 0, 400, 400)
11
12 .AddPath(p => p
13 .D(d => d
14 .M(80, 210)
15 .Q(95, 110, 200, 200)
16 .T(350, 200)
17 )
18 // Sets the fill color of the path to red
19 .Fill(Color.Red))
20 .Build(document.FirstChild as SVGSVGElement);
21
22 // Save the SVG document
23 document.Save(Path.Combine(OutputDir, "curve-path.svg"));
24}在此示例中:
Q() 创建二次 Bézier 曲线;T() 自动继续平滑曲线。Curve commands 常用于插图、图表、签名和装饰性 SVG 图形。
SVG 可以沿 <path> 元素定义的 path 放置文本。这通过带有 href 属性的 <textPath> 元素实现。
1using Aspose.Svg.Builder;
2using System.Drawing;
3using System.IO; 1// Create SVG text path in C# using SVG Builder
2
3// Initialize an SVG document
4using (SVGDocument document = new SVGDocument())
5{
6 // Create an <svg> element with specified width, height, and viewBox, and add into it <path> elements
7 SVGSVGElement svg = new SVGSVGElementBuilder()
8 .Width(1200).Height(300)
9 .ViewBox(0, 0, 1200, 300)
10
11 .AddPath(p => p.Id("Path1")
12 .Fill(f => f.None()).Stroke(Color.IndianRed).Transform(t => t.Translate(-100, 40))
13 .D(d => d.AddPathSegment("M 199 89 C 206 66 235 25 270 30 286 38 298 59 310 73 321 87 338 99 356 103 387 111 396 90 410 85"))
14 )
15
16 .AddPath(p => p.Id("Path2")
17 .Fill(f => f.None()).Stroke(Paint.None).Transform(t => t.Translate(400, -100))
18 .D(d => d.M(80, 210).Q(95, 110, 200, 200).T(400, 200)))
19
20
21 .AddText(t => t.FontSize(30).Fill(Color.Teal)
22 .AddTextPath(tp => tp
23 .Href("#Path1")
24 .AddContent("SVG textPath element")
25 )
26 .AddTextPath(tp => tp
27 .Href("#Path2")
28 .AddContent("SVG can place text along a path")
29 )
30 )
31
32 .Build(document.FirstChild as SVGSVGElement);
33
34 // Save the SVG document
35 document.Save(Path.Combine(OutputDir, "text-path.svg"));
36}在示例中,我们创建两个具有不同 id 属性的 paths。第一个 path 使用 AddPathSegment(),第二个 path 使用 M()、Q() 和 T()。SVG <textPath> 元素引用 <path> 的 id,从而沿该 path 显示文本:

这里也使用 builders within builders: SVGSVGElementBuilder 创建 <svg>,嵌套 builders 添加 <path>、<text> 和 <textPath>。
| 问题 | 可能原因 | 建议修复 |
|---|---|---|
| SVG path 不可见 | 缺少 fill 或 stroke 样式 | 应用 Fill() 或 Stroke() |
| Path 形状不完整 | Path 未关闭 | 使用 Z() |
| 文本没有沿曲线排列 | Path 引用不正确 | 确保 Href() 与 Id() 匹配 |
| 曲线显示变形 | 控制点坐标不正确 | 调整 Bézier 曲线控制点 |
| 导入的 path data 无法正确渲染 | SVG path 语法无效 | 导入前验证 SVG path 字符串 |
1. 创建简单三角形
1using (SVGDocument document = new SVGDocument())
2{
3 SVGSVGElement svg = new SVGSVGElementBuilder()
4 .Width(200).Height(200)
5 .ViewBox(0, 0, 150, 150)
6 .AddPath(p => p
7 .D(d => d
8 .M(50, 150)
9 .L(150, 150)
10 .L(100, 50)
11 .Z())
12 .Fill(Color.Teal))
13 .Build(document.FirstChild as SVGSVGElement);
14 document.Save("triangle.svg");
15}2. 添加 path segment
1 .AddPath(p => p
2 .Fill(Color.IndianRed)
3 .D(d => d.AddPathSegment("M10 75 Q150 0 290 75"))
4 )3. 组合多个 segments
1 .AddPath(p => p
2 .Fill(Color.CadetBlue)
3 .Stroke(Color.Red)
4 .StrokeWidth(2)
5 .D(d => d
6 .AddPathSegment("M40 120")
7 .AddPathSegment("C70 20 140 20 170 120")
8 .AddPathSegment("S270 220 300 120")
9 .AddPathSegment("Q250 180 170 170")
10 .AddPathSegment("Q90 160 40 120")
11 )
12 )Note: 第一个 recipe 与本文前面显示的 using directives 一起使用时是完整示例。其他 recipes 是应整合到完整 SVG 创建流程中的代码片段。
1. PathBuilder 是否比手写 SVG path strings 更好?
对于复杂 SVG 图形,PathBuilder 通常比手动拼接 path strings 更易维护和调试。
2. 为什么使用 SVG paths 而不是基本 shapes?
SVG paths 比 <rect> 或 <circle> 更灵活,可表示复杂矢量图形、曲线和复合形状。
3. 可以将现有 SVG path data 转换为 PathBuilder commands 吗?
可以。现有 d 属性值可转换为 fluent PathBuilder commands,或使用 AddPathSegment() 直接插入。
4. SVG paths 是否与分辨率无关?
是。SVG paths 是矢量图形,可无损缩放。
5. 一个 SVG path 可以包含多个不连续形状吗?
可以。通过在同一个 path 中使用多个 M commands,可以包含多个 subpaths。
6. 相对和绝对 SVG path commands 有什么区别?
绝对 commands 使用固定 SVG 坐标,相对 commands 根据当前绘制点计算位置。
stroke 和 fill 值的 PaintBuilder。Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.