Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
In SVG, transformations are used to modify the position, size, orientation, and skewing of SVG elements within the SVG canvas. SVG objects can be transformed using the transform attribute’s properties: translate, scale, rotate, skewX, skewY, and matrix. Using Aspose.SVG for .NET library, you can perform these transformations programmatically. In this chapter is a general overview of how you might work with transformations using Aspose.SVG.
The current chapter describes popular SVG transforms, as well as C# examples for most common transformation scenarios.
transform attribute as well as a transformation matrix matrix(a,b,c,d,e,f).transform Attribute Properties. Transform attribute properties in SVG allow you to manipulate elements using the following transform functions:transform attribute. Only the first 6 values of the matrix can be specified to set translation, scaling, rotation, and skewing.These two ways offer different levels of complexity and precision, with the transform attribute being easier for simple transformations while the transformation matrix allows greater control over the transformation process. Let’s look at these methods using C# examples for an SVG transformation, such as SVG translation.
The following C# code snippet demonstrates how to create an SVG <rect> element, set its attributes, and apply a translation transformation using the transform attribute. The transformation moves the rectangle by 150 units to the right and 50 units downward.
RootElement property points to the document’s root <svg> element.<rect> element with attributes and add it to the <svg> element:name, value) method to set attributes specifying position, size, fill, and transformation. The transform attribute is set on the <rect> element using the value "translate(150, 50)". This applies a translation transformation that moves the rectangle 150 units to the right and 50 units downward.rectElement to svgElement, you can use the
AppendChild() method.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}Here, we will look at the C# example like the one above – create an SVG <rect> element, set its attributes, and implement the translation using a transformation matrix. Let’s take a closer look at the C# code for implementing the transformation matrix.
<rect> element. The CTM represents the cumulative transformations applied to the element and includes information about translation, rotation, scaling, and skewing that can be applied to the element.transformAttribute – a string representation of a 2D transformation matrix using the values from the modified transformationMatrix. The matrix notation is matrix(a, b, c, d, e, f).transform attribute of the <rect> element using the transformAttribute string.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}You can download the complete examples and data files from GitHub.
About downloading from GitHub and running examples, you find out from the How to Run the Examples section.
To learn how to use a transformation matrix to rotate, scale, translate, and skew SVG graphics and consider SVG code examples, please go to the Transformation Matrix – SVG Code Examples article.
Read the SVG Transformation article to learn more and get SVG code examples for rotating, scaling, moving, and skewing SVG graphics using the SVG transform attribute.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.