# Apply Matrix Transformation

Learn how to reposition the graphics and alter their orientation and size using DsImaging. It may involve a series of operations such as rotation, scaling, etc.

## Content

Transformation plays a vital role when it comes to graphics. The purpose of using transformation in graphics is to reposition the graphics and alter their orientation and size. It may involve a sequence of operations such as, translation, scaling, rotation, etc..
DsImaging supports graphics transformation through [Transform](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmapGraphics.Transform.html) property of the [GcBitmapGraphics](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmapGraphics.html) class which is of type Matrix3x2. The **Matrix3x2** struct represents a 3x2 matrix and is a member of **System.Numerics** namespace. The transformations are applied in the order reverse to which they are added to the matrix.
![Matrix transformation applied on a rectangle shape which alters its orientation and size](https://cdn.mescius.io/document-site-files/images/a5680f0e-2d32-45dc-8048-d3ef11d0dae7/images/transformation.png)
To apply matrix transformation:

1. Initialize the GcBitmap class.
2. Create a drawing surface using [CreateGraphics](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmap.CreateGraphics.html) method of the **GcBitmap** class which returns an instance of the **GcBitmapGraphics** class.
3. Draw a rectangle using [DrawRectangle](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawRectangle.html) method and apply the background color using [FillRectangle](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.FillRectangle.html) method of the GcBitmapGraphics class.
4. Define the text to be rendered in a rectangle.
5. Add text to the rectangles using [DrawString](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Drawing.GcGraphics.DrawString.html) method of the GcBitmapGraphics class
6. Create a transformation matrix with different transformation types. For example, create rotation, translation, and scaling matrix using **CreateRotation**, **CreateTranslation** and **CreateScale** method of the Matrix3x2 class respectively.
7. Apply the transformation matrix using the **Transform** property.
    Note that the sequence of transformations applied to the text is done in reverse order, which means first is scaling followed by translation and rotation.

    ```csharp
    //Initialize GcBitmap
    GcBitmap origBmp = new GcBitmap(1024, 1024, true);
    
    //Create the graphics for the Bitmap
    GcBitmapGraphics g = origBmp.CreateGraphics(Color.White);
    
    //Define text to be rendered in box/rectangle
    const string baseTxt = "Text drawn at (10,36) in a 4\"x2\" box";
    var Inch = origBmp.DpiX;
    
    // Render the image with tranformed text
    // Transforms are applied in order from last to first.]
    var rotate = Matrix3x2.CreateRotation((float)(-70 * Math.PI) / 180f);
    var translate = Matrix3x2.CreateTranslation(Inch * 3, Inch * 5);
    var scale = Matrix3x2.CreateScale(0.7f);
    
    g.Transform =
        rotate *
        translate *
        scale;
    
    var box = new RectangleF(10, 36, origBmp.DpiX * 4, origBmp.DpiY * 2);
    g.FillRectangle(box, Color.FromArgb(80, 0, 184, 204));
    g.DrawRectangle(box, Color.FromArgb(0, 193, 213), 1);
    box.Inflate(-6, -6);
    g.DrawString(baseTxt, new TextFormat()
    {
        Font = Font.FromFile(Path.Combine("Resources", "Fonts", 
        "times.ttf")),
        FontSize = 14,
    },
    box);
    
    //Save the image rendering different shapes
    origBmp.SaveAsJpeg("MatrixTransform.jpeg");
    ```

    For more information about using transformation matrix in DsImaging, see [DsImaging sample browser](https://developer.mescius.com/documents-api-imaging/demos/basics/drawing/graphics-transforms/code-cs).

    > !type=note
    > **Note**: For rendering large or complex text and graphics, you can use [Skia](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging.Skia/GrapeCity.Documents.Imaging.Skia.html) library. For more information about the library and its usage, see [Render using Skia Library](/document-solutions/dot-net-imaging-api/docs/online/render-using-skia).