Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
在 SVG 中,变换用于修改 SVG 画布内 SVG 元素的位置、大小、方向和倾斜。可以使用变换属性的属性来变换 SVG 对象:translate, scale, rotate, skewX, skewY 和 matrix。使用 Aspose.SVG for .NET 库,您可以通过编程方式执行这些转换。本章概述了如何使用 Aspose.SVG 进行转换。
当前章节介绍了流行的 SVG 转换,以及最常见转换场景的 C# 示例。
transform属性。 SVG 中的变换属性允许您使用以下变换函数来操作元素:transform 属性的矩阵属性进行更改。只能指定矩阵的前 6 个值来设置平移、缩放、旋转和倾斜。这两种方式提供了不同级别的复杂性和精度,transform属性更容易进行简单变换,而变换矩阵允许对变换过程进行更好的控制。让我们使用 C# 示例来了解这些用于 SVG 转换的方法,例如 SVG 翻译。
以下 C# 代码片段演示了如何创建 SVG <rect> 元素、设置其属性以及使用 transform 属性应用平移转换。变换将矩形向右移动 150 个单位,向下移动 50 个单位。
RootElement 属性指向文档的根 <svg> 元素。<rect> 元素并将其添加到 <svg> 元素中:name, value) 方法设置指定位置、大小、填充和变换的属性。使用值translate(150, 50)在 <rect>元素上设置 transform属性。这将应用平移变换,将矩形向右移动 150 个单位,向下移动 50 个单位。rectElement 添加到 svgElement,可以使用
AppendChild() 方法。1using Aspose.Svg;
2using System.IO; 1// Translate an SVG rectangle using the transform attribute in C#
2
3// Create a new SVG document
4using (SVGDocument document = new SVGDocument())
5{
6 SVGSVGElement svgElement = document.RootElement;
7
8 // Create a <rect> element and set its attributes
9 SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS("http://www.w3.org/2000/svg", "rect");
10 rectElement.X.BaseVal.Value = 50;
11 rectElement.Y.BaseVal.Value = 50;
12 rectElement.Width.BaseVal.Value = 100;
13 rectElement.Height.BaseVal.Value = 100;
14 rectElement.SetAttribute("fill", "blue");
15
16 // Apply translate() function to the rectangle
17 rectElement.SetAttribute("transform", "translate(150, 50)");
18
19 // Append the <rect> element to the <svg> element
20 svgElement.AppendChild(rectElement);
21
22 // Save the document
23 document.Save(Path.Combine(OutputDir, "translate.svg"));
24}在这里,我们将看一下上面的 C# 示例 – 创建一个 SVG <rect> 元素,设置其属性,并使用转换矩阵实现转换。让我们仔细看看实现变换矩阵的 C# 代码。
<rect> 元素关联的当前变换矩阵 (CTM)。 CTM 表示应用于元素的累积变换,并包括有关可应用于元素的平移、旋转、缩放和倾斜的信息。transformAttribute – 使用修改后的“transformationMatrix”中的值构建 2D 变换矩阵的字符串表示形式。矩阵表示法为matrix(a, b, c, d, e, f)。transformAttribute字符串设置 <rect>元素的 transform属性。1using Aspose.Svg;
2using System.IO; 1// Translate an SVG element using a transformation matrix in C#
2
3// Create a new SVG document
4using (SVGDocument document = new SVGDocument())
5{
6 SVGSVGElement svgElement = document.RootElement;
7
8 // Create a <rect> element and set its attributes
9 SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS("http://www.w3.org/2000/svg", "rect");
10 rectElement.X.BaseVal.Value = 150;
11 rectElement.Y.BaseVal.Value = 50;
12 rectElement.Width.BaseVal.Value = 100;
13 rectElement.Height.BaseVal.Value = 100;
14 rectElement.SetAttribute("fill", "blue");
15 svgElement.AppendChild(rectElement);
16
17 // Get the current transformation matrix associated with the rectElement
18 SVGMatrix transformationMatrix = rectElement.GetCTM();
19 transformationMatrix = transformationMatrix.Translate(150, 50);
20
21 // Apply the transformation matrix to the rectElement
22 string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
23 + transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
24 + transformationMatrix.F + ")";
25 rectElement.SetAttribute("transform", transformAttribute);
26
27 // Save the document
28 document.Save(Path.Combine(OutputDir, "translation-matrix.svg"));
29}您可以从
GitHub 下载完整的示例和数据文件。
关于从 GitHub 下载并运行示例,您可以从
如何运行示例 部分找到。
要了解如何使用变换矩阵来旋转、缩放、平移和倾斜 SVG 图形并考虑 SVG 代码示例,请转到
变换矩阵 – SVG 代码示例 文章。
阅读
SVG 变换 文章了解更多信息并获取使用 SVG 变换属性旋转、缩放、移动和倾斜 SVG 图形的 SVG 代码示例。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.