旋转 SVG – C# 示例

如何旋转 SVG – C# 代码

使用 Aspose.SVG for .NET 库,您可以通过编程方式执行旋转转换。本文介绍了 SVG 旋转的 C# 示例。考虑在 transform属性中使用 rotate() 函数以及变换矩阵 matrix(a,b,c,d,e,f) 的情况。

旋转()函数

以下 C# 代码片段演示了如何创建 SVG <rect> 元素、设置其属性,并使用rotate() 函数对 transform 属性应用转换。

  1. 创建 SVGDocument 类的实例。 RootElement 属性指向文档的根 <svg> 元素。
  2. 创建一个带有属性的 <rect> 元素并将其添加到 <svg> 元素中:
    • 您可以使用 CreateElementNS() 方法创建 SVGRectElement 类的实例。
    • 调用 SetAttribute() 方法设置指定位置、大小、填充和变换的属性。使用 transform属性的rotate()函数,它获取要旋转的角度和要旋转的点的坐标。如果不指定坐标,则围绕初始坐标系的点(0, 0)进行旋转。
    • 要将 rectElement 添加到 svgElement,您可以使用 AppendChild() 方法。
  3. 调用 Save()方法将生成的SVG图像保存到路径指定的本地文件中。
1using Aspose.Svg;
2using System.IO;
 1// Rotate an SVG element using the transform attribute programmatically with Aspose.SVG
 2
 3// Set SVG Namespace URL
 4string SvgNamespace = "http://www.w3.org/2000/svg";
 5
 6// Create a new SVG document
 7using (SVGDocument document = new SVGDocument())
 8{
 9    SVGSVGElement svgElement = document.RootElement;
10
11    // Create a <rect> element and set its attributes
12    SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS(SvgNamespace, "rect");
13    rectElement.X.BaseVal.Value = 150;
14    rectElement.Y.BaseVal.Value = 50;
15    rectElement.Width.BaseVal.Value = 100;
16    rectElement.Height.BaseVal.Value = 100;
17    rectElement.SetAttribute("fill", "purple");
18
19    // Apply rotate() function to the SVG
20    rectElement.SetAttribute("transform", "rotate(45)");
21
22    // Append the rect element to the SVG
23    svgElement.AppendChild(rectElement);
24
25    // Save the document
26    document.Save(Path.Combine(OutputDir, "rotate-svg-rect.svg"));
27}
Example-RotateSvg.cs hosted with ❤ by GitHub

该变换将矩形围绕原始坐标系的点 (0, 0) 旋转 45 度,因为元素需要围绕其旋转的点的坐标未传递给rotate() 函数。为了清楚起见,该图显示了旋转之前和之后的坐标系。这说明旋转变换是通过旋转坐标系来完成的。

文本“原始矩形 (a) 和旋转矩形的图像 (b)。”

旋转 SVG 元素 – SVG 绕中心旋转

以下 C# 代码片段演示了如何在现有 SVG 文件中查找所需的 SVG 元素并旋转它。您应该遵循以下几个步骤:

  1. 使用 SVGDocument() 构造函数之一加载源 SVG 文件。
  2. 获取文档的根<svg>元素。
  3. 使用 QuerySelector() 方法查找第一个要旋转的矩形元素。 Element 类的 QuerySelector(selector) 方法允许您获取文档中与指定选择器匹配的第一个元素。
  4. 为矩形元素设置 transform属性,值为rotate(45, 100, 140)。此变换将元素围绕坐标为 (100, 140) 的指定点(选定矩形的中心)旋转 45 度角(顺时针)。
  5. 调用 Save()方法将生成的SVG图像保存到本地文件。
1using Aspose.Svg;
2using System.IO;
 1// Rotate a single selected element in an SVG document using C#
 2
 3// Load an SVG document from a file
 4SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));
 5
 6// Get the root SVG element of the document
 7SVGSVGElement svgElement = document.RootElement;
 8
 9// Get the fist <rect> element for rotation
10SVGRectElement rectElement = svgElement.QuerySelector("rect") as SVGRectElement;
11
12// Set a new "transform" attribute with rotation value for the rectangle element
13rectElement.SetAttribute("transform", "rotate(45, 100, 140)");
14
15// Save the document
16string outputPath = "rotate-element.svg";
17document.Save(Path.Combine(OutputDir, outputPath));

下图显示了原始 SVG (a) 和带有旋转的第一个矩形元素的图像 (b)。此转换改变了矩形相对于其他元素的外观。矩形的位置、宽度和高度保持不变,但视觉上它将围绕指定点(在本例中为矩形的中心)旋转。

Text “原始 svg 图像 (a) 和旋转第一个矩形元素的图像 (b)。”

注意:要围绕其中心或某个其他点旋转 SVG 元素,必须使用 rotate(angle, cx, cy) 函数,该函数将元素围绕坐标 (cx, cy) 处的点旋转一定角度。 确定(计算)图像中心的位置(其坐标)并执行旋转。

旋转 SVG 元素 – 使用 SVG Builder

在这里,我们将看相同的示例:围绕其中心旋转 shapes.svg 文件中的第一个<rect>元素。不过,这一次,我们将使用 SVG Builder 来完成此任务。 SVG Builder API 为开发人员提供了一个强大的工具,用于以简化的方式创建和更新 SVG 元素。在此代码片段中:

1using Aspose.Svg.Builder;
2using System.Linq;
3using System.IO;
 1// Rotate a single selected element in an SVG document using SVG Builder API
 2
 3// Load an SVG document
 4using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg")))
 5{
 6    // Find the first <rect> element for rotation
 7    SVGRectElement rect = document.GetElementsByTagName("rect").First() as SVGRectElement;
 8
 9    // Rotate the first <rect> element around its center using the SVGRectElementBuilder
10    new SVGRectElementBuilder()
11        .Transform(t => t.Rotate(45, 100, 140))
12        .Build(rect);
13
14    // Save the document
15    document.Save(Path.Combine(OutputDir, "rotate-element-using-builder.svg"));
16}

Transform() 方法是 SVG Builder API 的一部分,它允许开发人员以编程方式将转换应用于 SVG 元素。 SVG 中的转换使开发人员能够修改 SVG 元素的位置、旋转、缩放和倾斜。 Transform()方法通常采用一个或多个转换函数作为参数,每个函数指定一种特定类型的转换。这些功能包括:

Transform()方法允许链接多个转换函数以应用更复杂的转换。

文章 SVG Builder API 探讨了 Aspose.SVG Builder API 在 C# 中创建和修改 SVG 元素的功能。 SVG Builder API 旨在简化 C# 中 SVG 元素的创建和更新。您将看到 Fluent Builder Pattern 和 mixin 在 SVG 操作中的效率。您将了解类和方法、专门的构建器以及它们如何简化 SVG 编程。

SVG 变换矩阵 – 旋转矩阵

在这里,我们将查看用于旋转整个 SVG 图像而不是单个元素的 C# 示例,并使用旋转矩阵实现转换。让我们仔细看看应用旋转矩阵的 C# 代码。

  1. 使用 SVGDocument() 构造函数之一加载源 SVG 文件。
  2. 获取文档的根<svg>元素。
  1. 使用 SVGGraphicsElement 类的 GetCTM() 方法返回与 <svg> 元素关联的当前变换矩阵 (CTM)。 CTM 表示应用于元素的累积变换,并包括有关可应用于元素的平移、旋转、缩放和倾斜的信息。
  2. 获得 CTM 后,使用 Rotate() 方法在当前矩阵上后乘旋转变换并返回结果矩阵。 Rotate(angle) 方法仅采用一个参数 – 角度。此参数是强制性的,表示您要应用的旋转量。
  3. 构造一个 transformAttribute – 使用修改后的变换矩阵transformationMatrix中的值构建 2D 变换矩阵的字符串表示形式。矩阵表示法为matrix(a, b, c, d, e, f)
  4. 调用 SetAttribute() 方法,使用 transformAttribute字符串设置<svg>元素的 transform属性。
  5. 调用 Save()方法将生成的SVG图像保存到本地文件。
1using Aspose.Svg.Builder;
2using System.Linq;
3using System.IO;
 1// Rotate an entire SVG document using a transformation matrix programmatically in C#
 2
 3// Load an SVG document
 4string documentPath = Path.Combine(DataDir, "shapes.svg");
 5using (SVGDocument document = new SVGDocument(documentPath))
 6{
 7    SVGSVGElement svgElement = document.RootElement;
 8
 9    // Get the transformation matrix associated with the svgElement
10    SVGMatrix transformationMatrix = svgElement.GetCTM();
11    transformationMatrix = transformationMatrix.Rotate(45);
12
13    // Apply the transformation matrix to the svgElement
14    string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
15                                + transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
16                                + transformationMatrix.F + ")";
17    svgElement.SetAttribute("transform", transformAttribute);
18
19    // Save the document
20    document.Save(Path.Combine(OutputDir, "rotate-matrix.svg"));
21}

下图显示了原始 SVG (a) 和旋转图像 (b)。

文本“该图显示原始 SVG (a) 和旋转图像 (b)。”

也可以看看

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.