Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/d6f4869876ef0ce448192ca0c5fd4e95 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/d6f4869876ef0ce448192ca0c5fd4e95 to your computer and use it in GitHub Desktop.
This Gist contains C# examples Aspose.SVG for .NET – SVG Transformations

Aspose.SVG for .NET – Rotate, Translate, Scale, Skew, and Crop SVG in C#

This GitHub gist repository contains C# code examples used in the Aspose.HTML for .NET documentation, specifically within the SVG Transformations – Rotate, Scale, and Translate SVG chapter. The snippets show how to rotate, translate, scale, skew, crop, and combine transformations with transform attributes, SVG matrices, and the SVG Builder API.

Topics Covered

  • Rotate SVG Elements and Shapes – Rotate SVG shapes using the rotate() transform, transformation matrices, or the SVG Builder API.
  • Scale SVG Content – Apply scale() transformations to entire documents or individual elements.
  • Translate SVG Elements – Move shapes with the translate() transform or an SVG transformation matrix.
  • Skew and Crop SVG – Skew SVG content with transform functions and crop SVG images by changing viewBox.
  • Transformation Matrices – Combine scale, translate, and rotate operations with SVG matrix values.
  • SVG Builder API – Use SVGSVGElementBuilder and SVGRectElementBuilder to apply transformations in fluent C# code.

Quick Start

  1. Make sure you have the Aspose.SVG for .NET library installed.
  2. Locate the example that matches your SVG transformation task.
  3. Open and adjust the example files with your paths and file names.
  4. Run the code to create transformed SVG output files.

About Aspose.SVG for .NET

Aspose.SVG for .NET is a cross-platform library enabling full control over SVG graphics in C# applications. It provides an API for creating, editing, saving, and converting SVG content without requiring external software. With DOM manipulation, SVG Builder, and transformation matrix support, it helps automate vector graphics tasks such as rotation, scaling, translation, skewing, cropping, and conversion.

Prerequisites

To run the examples, you need:

  • .NET Framework 4.6.1+, .NET Core 2.0+, or .NET 5+.
  • Supported OS: Windows, Linux, macOS.
  • The Aspose.SVG for .NET library (you can install it via NuGet: Install-Package Aspose.SVG).

Resources & Support

Aspose.SVG for .NET – SVG Transformations
// Combine scale, translate, and rotate SVG transformations using a matrix in C#
// Learn more: https://docs.aspose.com/svg/net/svg-scaling/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "snowflake.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Get the transformation matrix associated with the svgElement
SVGMatrix transformationMatrix = svgElement.GetCTM();
transformationMatrix = transformationMatrix.Scale(0.5F)
.Translate(250, 250)
.Rotate(20);
// Apply the transformation matrix to the svgElement
string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
+ transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
+ transformationMatrix.F + ")";
svgElement.SetAttribute("transform", transformAttribute);
// Save the document
document.Save(Path.Combine(OutputDir, "scale-image.svg"));
}
// Crop an SVG image by updating the viewBox attribute in C#
// Learn more: https://docs.aspose.com/svg/net/svg-transformations/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Define the new view box (x, y, width, height) for cropping
double newX = 20; // New x-coordinate of the top-left corner
double newY = 20; // New y-coordinate of the top-left corner
double newWidth = 200; // New width of the cropped area
double newHeight = 220; // New height of the cropped area
// Update the viewBox attribute
string viewBox = $"{newX} {newY} {newWidth} {newHeight}";
document.RootElement.SetAttribute("viewBox", viewBox);
// Save the document
document.Save(Path.Combine(OutputDir, "crop-image.svg"));
}
// Rotate an SVG rectangle using the transform attribute in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Set SVG Namespace URL
string SvgNamespace = "http://www.w3.org/2000/svg";
// Create a new SVG document
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svgElement = document.RootElement;
// Create a <rect> element and set its attributes
SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS(SvgNamespace, "rect");
rectElement.X.BaseVal.Value = 150;
rectElement.Y.BaseVal.Value = 50;
rectElement.Width.BaseVal.Value = 100;
rectElement.Height.BaseVal.Value = 100;
rectElement.SetAttribute("fill", "purple");
// Apply rotate() function to the SVG
rectElement.SetAttribute("transform", "rotate(45)");
// Append the rect element to the SVG
svgElement.AppendChild(rectElement);
// Save the document
document.Save(Path.Combine(OutputDir, "rotate-svg-rect.svg"));
}
// Rotate an SVG document using a transformation matrix in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Get the transformation matrix associated with the svgElement
SVGMatrix transformationMatrix = svgElement.GetCTM();
transformationMatrix = transformationMatrix.Rotate(45);
// Apply the transformation matrix to the svgElement
string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
+ transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
+ transformationMatrix.F + ")";
svgElement.SetAttribute("transform", transformAttribute);
// Save the document
document.Save(Path.Combine(OutputDir, "rotate-matrix.svg"));
}
// Rotate a selected SVG rectangle around a point in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Load an SVG document from a file
SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));
// Get the root SVG element of the document
SVGSVGElement svgElement = document.RootElement;
// Get the first <rect> element for rotation
SVGRectElement rectElement = svgElement.QuerySelector("rect") as SVGRectElement;
// Set a new "transform" attribute with rotation value for the rectangle element
rectElement.SetAttribute("transform", "rotate(45, 100, 140)");
// Save the document
string outputPath = "rotate-element.svg";
document.Save(Path.Combine(OutputDir, outputPath));
// Create rotated SVG shapes and text using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Create a new SVG document
using (var document = new SVGDocument())
{
var svgElement = document.RootElement as SVGSVGElement;
// Build the SVG with rotated elements using TransformBuilder
new SVGSVGElementBuilder()
.Width(800)
.Height(600)
.Fill(Color.FromArgb(245, 245, 245))
.AddDefs(defs => defs
.AddLinearGradient(gradient =>
{
gradient.Id("skyGradient");
gradient.X1(0);
gradient.Y1(0);
gradient.X2(0);
gradient.Y2(1);
gradient.AddStop(stop =>
{
stop.Offset(0);
stop.StopColor(Color.FromArgb(33, 150, 243));
});
gradient.AddStop(stop =>
{
stop.Offset(1);
stop.StopColor(Color.FromArgb(144, 202, 249));
});
})
)
.AddRect(rect => rect
.X(0).Y(0).Width(800).Height(600)
.Attribute("fill", "url(#skyGradient)")
)
// Add a rectangle that will be rotated
.AddRect(rect => rect
.X(100).Y(100).Width(200).Height(150)
.Fill(Color.FromArgb(255, 152, 0))
.Stroke(Color.FromArgb(25, 118, 210))
.StrokeWidth(3)
.Transform(transform => transform
.Rotate(45, 200, 175) // Rotate 45 degrees around center point (200, 175)
)
)
// Add a circle that will be rotated
.AddCircle(circle => circle
.Cx(500).Cy(200).R(80)
.Fill(Color.FromArgb(76, 175, 80))
.Stroke(Color.FromArgb(0, 121, 107))
.StrokeWidth(4)
.Transform(transform => transform
.Rotate(90, 500, 200) // Rotate 90 degrees around center point (500, 200)
)
)
// Add an ellipse with multiple transformations including rotation
.AddEllipse(ellipse => ellipse
.Cx(300).Cy(400).Rx(100).Ry(60)
.Fill(Color.FromArgb(156, 39, 176))
.Stroke(Color.FromArgb(103, 58, 183))
.StrokeWidth(3)
.Transform(transform => transform
.Rotate(135, 300, 400) // Rotate 135 degrees around center point (300, 400)
)
)
// Add a path with rotation
.AddPath(path => path
.D(d =>
{
d.M(600, 400);
d.L(700, 400);
d.L(650, 500);
d.Z();
})
.Fill(Color.FromArgb(255, 87, 34))
.Stroke(Color.FromArgb(183, 28, 28))
.StrokeWidth(3)
.Transform(transform => transform
.Rotate(60, 650, 466) // Rotate 60 degrees around center point (650, 466)
)
)
// Add text with rotation – using Attribute for text content
.AddText(text => text
.X(400).Y(550)
.FontSize(32)
.FontFamily("Arial")
.Fill(Color.FromArgb(33, 33, 33))
.Transform(transform => transform
.Rotate(180, 400, 550) // Rotate 180 degrees around point (400, 550)
)
.Attribute("xml:space", "preserve")
)
.Build(svgElement);
// Set text content using DOM API since Builder doesn't have TextContent method
var textElement = svgElement.GetElementsByTagName("text")[0] as SVGTextElement;
if (textElement != null)
{
textElement.TextContent = "Rotated Text Example";
}
// Save the SVG document
document.Save(Path.Combine(OutputDir, "rotated-elements.svg"));
}
// Rotate a selected SVG rectangle using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Load an SVG document
using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg")))
{
// Find the first <rect> element for rotation
SVGRectElement rect = document.GetElementsByTagName("rect").First() as SVGRectElement;
// Rotate the first <rect> element around its center using the SVGRectElementBuilder
new SVGRectElementBuilder()
.Transform(t => t.Rotate(45, 100, 140))
.Build(rect);
// Save the document
document.Save(Path.Combine(OutputDir, "rotate-element-using-builder.svg"));
}
// Rotate an SVG document using SVGElementBuilder in C#
// Learn more: https://docs.aspose.com/svg/net/rotate-svg/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Transform(t => t.Rotate(45))
.Build(document.FirstChild as SVGSVGElement);
// Save the document
document.Save(Path.Combine(OutputDir, "rotate-shapes.svg"));
}
// Scale an SVG document using the SVG Builder API in C#
// Learn more: https://docs.aspose.com/svg/net/svg-scaling/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svg = new SVGSVGElementBuilder()
.Transform(t => t.Scale(0.5))
.Build(document.FirstChild as SVGSVGElement);
// Save the document
document.Save(Path.Combine(OutputDir, "scale-shapes.svg"));
}
// Scale an entire SVG image by transforming the root SVG element in C#
// Learn more: https://docs.aspose.com/svg/net/svg-scaling/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "tree.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Set a "transform" attribute with scale value for the <svg> element
svgElement.SetAttribute("transform", "scale(0.7)");
// Save the document
document.Save(Path.Combine(OutputDir, "scale-tree.svg"));
}
// Scale an SVG image using a transformation matrix in C#
// Learn more: https://docs.aspose.com/svg/net/svg-scaling/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "snowflake.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Get the transformation matrix associated with the svgElement
SVGMatrix transformationMatrix = svgElement.GetCTM();
transformationMatrix = transformationMatrix.Scale(0.5F);
// Apply the transformation matrix to the svgElement
string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
+ transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
+ transformationMatrix.F + ")";
svgElement.SetAttribute("transform", transformAttribute);
// Save the document
document.Save(Path.Combine(OutputDir, "scale-snowflake.svg"));
}
// Skew an SVG image using the skewX transform in C#
// Learn more: https://docs.aspose.com/svg/net/svg-transformations/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "flower.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Set a "transform" attribute with the required transform functions for the <svg> element
svgElement.SetAttribute("transform", "skewX(45)");
// Save the document
document.Save(Path.Combine(OutputDir, "transformed-flower.svg"));
}
// Scale an SVG circle using the transform attribute in C#
// Learn more: https://docs.aspose.com/svg/net/svg-scaling/
// Create a new SVG document
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svgElement = document.RootElement;
// Create a <circle> element and set its attributes
SVGCircleElement circleElement = (SVGCircleElement)document.CreateElementNS("http://www.w3.org/2000/svg", "circle");
circleElement.Cx.BaseVal.Value = 150;
circleElement.Cy.BaseVal.Value = 150;
circleElement.R.BaseVal.Value = 50;
circleElement.SetAttribute("fill", "salmon");
// Apply scaling to the SVG circle
circleElement.SetAttribute("transform", "scale(2)");
// Append the <circle> element to the SVG
svgElement.AppendChild(circleElement);
// Save the document
document.Save(Path.Combine(OutputDir, "scale-circle.svg"));
}
// Move an SVG rectangle using a transformation matrix in C#
// Learn more: https://docs.aspose.com/svg/net/svg-transformations/
// Create a new SVG document
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svgElement = document.RootElement;
// Create a <rect> element and set its attributes
SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS("http://www.w3.org/2000/svg", "rect");
rectElement.X.BaseVal.Value = 150;
rectElement.Y.BaseVal.Value = 50;
rectElement.Width.BaseVal.Value = 100;
rectElement.Height.BaseVal.Value = 100;
rectElement.SetAttribute("fill", "blue");
svgElement.AppendChild(rectElement);
// Get the current transformation matrix associated with the rectElement
SVGMatrix transformationMatrix = rectElement.GetCTM();
transformationMatrix = transformationMatrix.Translate(150, 50);
// Apply the transformation matrix to the rectElement
string transformAttribute = "matrix(" + transformationMatrix.A + "," + transformationMatrix.B + ","
+ transformationMatrix.C + "," + transformationMatrix.D + "," + transformationMatrix.E + ","
+ transformationMatrix.F + ")";
rectElement.SetAttribute("transform", transformAttribute);
// Save the document
document.Save(Path.Combine(OutputDir, "translation-matrix.svg"));
}
// Move an SVG rectangle using the translate transform in C#
// Learn more: https://docs.aspose.com/svg/net/svg-transformations/
// Create a new SVG document
using (SVGDocument document = new SVGDocument())
{
SVGSVGElement svgElement = document.RootElement;
// Create a <rect> element and set its attributes
SVGRectElement rectElement = (SVGRectElement)document.CreateElementNS("http://www.w3.org/2000/svg", "rect");
rectElement.X.BaseVal.Value = 50;
rectElement.Y.BaseVal.Value = 50;
rectElement.Width.BaseVal.Value = 100;
rectElement.Height.BaseVal.Value = 100;
rectElement.SetAttribute("fill", "blue");
// Apply translate() function to the rectangle
rectElement.SetAttribute("transform", "translate(150, 50)");
// Append the <rect> element to the <svg> element
svgElement.AppendChild(rectElement);
// Save the document
document.Save(Path.Combine(OutputDir, "translate.svg"));
}
// Move a selected SVG path element using the translate transform in C#
// Learn more: https://docs.aspose.com/svg/net/svg-transformations/
// Load an SVG document
string documentPath = Path.Combine(DataDir, "lineto.svg");
using (SVGDocument document = new SVGDocument(documentPath))
{
SVGSVGElement svgElement = document.RootElement;
// Get the first <path> element for translation
SVGPathElement pathElement = svgElement.QuerySelector("path") as SVGPathElement;
// Set a new "transform" attribute with translation value for the <path> element
pathElement.SetAttribute("transform", "translate(80)");
// Save the document
document.Save(Path.Combine(OutputDir, "translate-path-element.svg"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment