SVG Scaling – C# Examples
How to Scale SVG – C# Code
Using
Aspose.SVG for .NET library, you can perform a scale transformation programmatically. This article covers C# examples for SVG scaling. It considers cases of using the scale()
function in the transform
attribute as well as a transformation matrix – matrix(a,b,c,d,e,f)
.
Scale() Function
Scaling is an SVG transformation that enlarges or reduces an object using a scaling factor. You have to distinguish the uniform and directional scaling. The scale(sx, sy)
transform function allows scaling images along the x- and y-axis. The sy
scaling factor value is optional, and if omitted, it is assumed to be equal to sx
.
The following C# code snippet demonstrates how to create an SVG <circle>
element, set its attributes, and apply a transformation using the scale()
function for transform
attribute.
- Create an instance of the
SVGDocument class. The
RootElement
property points to the document’s root<svg>
element. - Create a
<circle>
element and set the required attributes.- You can use the CreateElementNS() method to create an instance of the SVGCircleElement class.
- Call the
SetAttribute() method to set attributes specifying position, size, fill, and transformation. Use the
scale()
function for thetransform
attribute that specifies a scaling transformation. In particular,scale(2)
means to scale the<circle>
element by a factor of 2 in both the x and y dimensions.
- To add the
circleElement
tosvgElement
, you can use the AppendChild() method. - Call the Save() method to save the resulting SVG image to a local file specified by path.
1using Aspose.Svg;
2using System.IO;
3...
4
5 // Create a new SVG document
6 using (var document = new SVGDocument())
7 {
8 var svgElement = document.RootElement;
9
10 // Create a rect element and set its attributes
11 var circleElement = (SVGCircleElement)document.CreateElementNS("http://www.w3.org/2000/svg", "circle");
12 circleElement.Cx.BaseVal.Value = 100;
13 circleElement.Cy.BaseVal.Value = 100;
14 circleElement.R.BaseVal.Value = 50;
15 circleElement.SetAttribute("fill", "salmon");
16
17 // Apply scaling to the SVG circle
18 circleElement.SetAttribute("transform", "scale(2)");
19
20 // Append the rect element to the SVG
21 svgElement.AppendChild(circleElement);
22
23 // Save the document
24 document.Save(Path.Combine(OutputDir, "scale-circle.svg"));
25 }
SVG Transform Matrix – Scale Matrix
Here, we will look at the C# example for scaling an entire SVG image rather than a single its element and implement the transformation using a scale matrix. Let’s look closer at the C# code for applying the scale matrix.
- Load a source SVG file using one of the SVGDocument() constructors.
- Get root
<svg>
element of the document.
- Note: To apply any transformation to the entire SVG image, you need to apply the
transform
attribute to the root<svg>
element.
- Use the
GetCTM() method of the SVGGraphicsElement class that returns the current transformation matrix (CTM) associated with the
<svg>
element. The CTM represents the cumulative transformations applied to the element and includes information about translation, scaling, scaling, and skewing that can be applied to the element. - After obtaining the CTM, use the Scale() method that post-multiplies a scale transformation on the current matrix and returns the resulting matrix. The Scale(scaleFactor) method takes only one argument – scaleFactor. This parameter is mandatory and represents the scaling in both x and y axis you want to apply.
- Construct a
transformAttribute
– a string representation of a 2D transformation matrix using the values from the modified transformation matrixtransformationMatrix
. The matrix notation is matrix(a, b, c, d, e, f). - Call the
SetAttribute() method to set the
transform
attribute of the<svg>
element using thetransformAttribute
string. - Call the Save() method to save the resulting SVG image to a local file.
1using Aspose.Svg;
2using System.IO;
3...
4
5 // Load an SVG document
6 string documentPath = Path.Combine(DataDir, "snowflake.svg");
7
8 using (var document = new SVGDocument(documentPath))
9 {
10 var svgElement = document.RootElement;
11
12 // Get the transformation matrix associated with the svgElement
13 var transformationMatrix = svgElement.GetCTM();
14 transformationMatrix = transformationMatrix.Scale(0.5F);
15
16 // Apply the transformation matrix to the svgElement
17 var transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
18 + transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
19 + transformationMatrix.F + ")";
20 svgElement.SetAttribute("transform", transformAttribute);
21
22 // Save the document
23 document.Save(Path.Combine(OutputDir, "scale-svg.svg"));
24 }
The following Figure shows the original SVG (a) and the scaled image with a scaling factor of 0.5 – the reduced image (b).
See Also
- 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.