Working with Coordinate System Transformations

Global Transformation

For global transformation of a scene in C#, the following steps can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Use the RotateTransform method of Graphics object to specify the rotation angle
  4. Draw the shapes with global transformation
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set a transformation that applies to every drawn item:
graphics.RotateTransform(15);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawEllipse(pen, 300, 300, 400, 200);
bitmap.Save("GlobalTransformation.png");

Global Transformation

Local Transformation

For local transformation of an object in C#, the following steps can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Create a shape such as Ellipse
  4. Define a matrix with desired transformation
  5. Apply the matrix to defined object
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
GraphicsPath path = new GraphicsPath();
path.AddEllipse(300, 300, 400, 200);
// Set a transformation that applies to the specific path to be drawn:
Matrix matrix = new Matrix();
matrix.RotateAt(45, new Point(500, 400));
path.Transform(matrix);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPath(pen, path);
bitmap.Save("LocalTransformation.png");

Local Transformation

Matrix Transformation

For Matrix transformation of a path in C#, the following steps can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Create a shape such as Rectangle
  4. Define the Matrix Transformation using the Matrix class
  5. Apply the transformation to defined rectangle
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
Rectangle originalRentangle = new Rectangle(300, 300, 300, 200);
TransformPath(graphics, originalRentangle, (matrix) => matrix.Rotate(15.0f));
TransformPath(graphics, originalRentangle, (matrix) => matrix.Translate(-250, -250));
TransformPath(graphics, originalRentangle, (matrix) => matrix.Scale(0.3f, 0.3f));
bitmap.Save("MatrixTransformations.png");
void TransformPath(Graphics graphics, Rectangle originalRentangle, Action<Matrix> transform)
{
GraphicsPath path = new GraphicsPath();
path.AddRectangle(originalRentangle);
Matrix matrix = new Matrix();
transform(matrix);
path.Transform(matrix);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawPath(pen, path);
}

Matrix Transformation

Page Transformation

For Page transformation of a scene in C#, the following steps can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Set the GraphicsUnit for the Graphics class object
  4. Draw a shape such as Rectangle
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set the transformation that maps page coordinates to device coordinates:
graphics.PageUnit = GraphicsUnit.Inch;
Pen pen = new Pen(Color.Blue, 0.1f);
graphics.DrawRectangle(pen, 1, 1, 1, 1);
bitmap.Save("PageTransformation.png");

Page Transformation

Units of Measure

To apply different units of measurement to different objects of a scene in C#, the following steps can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Set the PageUnit for the Graphics class object
  4. Draw the shapes such as Rectangles
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// 1 point is 1/72 inch.
graphics.PageUnit = GraphicsUnit.Point;
graphics.DrawRectangle(new Pen(Color.Red, 36f), 72, 72, 72, 72);
// 1 mm is 1/25.4 inch.
graphics.PageUnit = GraphicsUnit.Millimeter;
graphics.DrawRectangle(new Pen(Color.Green, 6.35f), 25.4f, 25.4f, 25.4f, 25.4f);
graphics.PageUnit = GraphicsUnit.Inch;
graphics.DrawRectangle(new Pen(Color.Blue, 0.125f), 1, 1, 1, 1);
bitmap.Save("UnitsOfMeasure.png");

Units of Measure

World Transformation

For World Transformation, the following sample C# code can be used.

  1. Instantiate a new object of Bitmap class
  2. Initialize a new object of Graphics class with this bitmap object
  3. Use the TranslateTransform method to set the transformation
  4. Draw a shape such as Rectangle
// For complete examples and data files, please go to https://github.com/aspose-drawing/Aspose.Drawing-for-.NET
using System.Drawing;
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Gray);
// Set the transformation that maps world coordinates to page coordinates:
graphics.TranslateTransform(500, 400);
Pen pen = new Pen(Color.Blue, 2);
graphics.DrawRectangle(pen, 0, 0, 300, 200);
bitmap.Save("WorldTransformation.png");

World Transformation