Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 23, 2021 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/e470c16479d8285a0f138b0c4bbc18c9 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e470c16479d8285a0f138b0c4bbc18c9 to your computer and use it in GitHub Desktop.
Apply Coordinate Transformations like Matrix, Global, Local, World Transformation Programmatically in C#
// Initialize Bitmap class object
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.FromKnownColor(KnownColor.Gray));
// Set a transformation that applies to every drawn item:
graphics.RotateTransform(15);
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 2);
graphics.DrawEllipse(pen, 300, 300, 400, 200);
// Save output image with Glabal transformation
bitmap.Save(dataDir + @"CoordinateSystemsTransformations\GlobalTransformation_out.png");
// Initialize Bitmap class object
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.FromKnownColor(KnownColor.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.FromKnownColor(KnownColor.Blue), 2);
graphics.DrawPath(pen, path);
// Save output image with Local transformation
bitmap.Save(dataDir + @"CoordinateSystemsTransformations\LocalTransformation_out.png");
// Initialize Bitmap class object
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
// Declare Graphics class object
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.FromKnownColor(KnownColor.Gray));
// Initiate Rectangle class object
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));
// Save output image with Matrix transformation
bitmap.Save(dataDir + @"CoordinateSystemsTransformations\MatrixTransformations_out.png");
private static 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.FromKnownColor(KnownColor.Blue), 2);
graphics.DrawPath(pen, path);
}
// Initialize Bitmap class object
Bitmap bitmap = new Bitmap(1000, 800, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.FromKnownColor(KnownColor.Gray));
// Set the transformation that maps world coordinates to page coordinates:
graphics.TranslateTransform(500, 400);
Pen pen = new Pen(Color.FromKnownColor(KnownColor.Blue), 2);
graphics.DrawRectangle(pen, 0, 0, 300, 200);
// Save output image with World Transformation
bitmap.Save(dataDir + @"CoordinateSystemsTransformations\WorldTransformation_out.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment