Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
使用
Aspose.SVG for .NET 库,您可以通过编程方式执行比例转换。本文介绍了用于 SVG 缩放的 C# 示例。考虑在 transform属性中使用 scale() 函数以及变换矩阵 matrix(a,b,c,d,e,f) 的情况。
缩放是一种 SVG 变换,它使用缩放因子放大或缩小对象。你必须区分均匀缩放和定向缩放。 scale(sx, sy)变换函数允许沿 x 轴和 y 轴缩放图像。 sy缩放因子值是可选的,如果省略,则假定等于sx。
以下 C# 代码片段演示了如何创建 SVG <circle> 元素、设置其属性,并使用 scale() 函数对 transform 属性应用转换。
RootElement 属性指向文档的根 <svg> 元素。<circle>元素并设置所需的属性。transform属性使用scale()函数。特别是,“scale(2)”表示将 <circle>元素在 x 和 y 维度上缩放 2 倍。circleElement 添加到 svgElement,可以使用
AppendChild() 方法。1using Aspose.Svg;
2using System.IO; 1// Scale an SVG circle using the transform attribute with Aspose.SVG
2
3// Create a new SVG document
4using (SVGDocument document = new SVGDocument())
5{
6 SVGSVGElement svgElement = document.RootElement;
7
8 // Create a <circle> element and set its attributes
9 SVGCircleElement circleElement = (SVGCircleElement)document.CreateElementNS("http://www.w3.org/2000/svg", "circle");
10 circleElement.Cx.BaseVal.Value = 150;
11 circleElement.Cy.BaseVal.Value = 150;
12 circleElement.R.BaseVal.Value = 50;
13 circleElement.SetAttribute("fill", "salmon");
14
15 // Apply scaling to the SVG circle
16 circleElement.SetAttribute("transform", "scale(2)");
17
18 // Append the <circle> element to the SVG
19 svgElement.AppendChild(circleElement);
20
21 // Save the document
22 document.Save(Path.Combine(OutputDir, "scale-circle.svg"));
23}在这里,我们将查看用于缩放整个 SVG 图像而不是单个元素的 C# 示例,并使用缩放矩阵实现转换。让我们仔细看看应用比例矩阵的 C# 代码。
<svg>元素。transform 属性应用到根 <svg> 元素。<svg> 元素关联的当前变换矩阵 (CTM)。 CTM 表示应用于元素的累积变换,并包括有关可应用于元素的平移、缩放、缩放和倾斜的信息。transformAttribute – 使用修改后的变换矩阵“transformationMatrix”中的值构建 2D 变换矩阵的字符串表示形式。矩阵表示法为matrix(a, b, c, d, e, f)。transformAttribute字符串设置 <svg>元素的 transform属性。1using Aspose.Svg;
2using System.IO; 1// Combine multiple SVG transformations (scale, translate, and rotate) using a transformation matrix with Aspose.SVG
2
3// Load an SVG document
4string documentPath = Path.Combine(DataDir, "snowflake.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.Scale(0.5F);
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, "scale-snowflake.svg"));
21}下图显示了原始 SVG (a) 和缩放因子为 0.5 的缩放图像 – 缩小图像 (b)。

Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.