Documents for Imaging .NET Edition
Features / Work with Graphics / Apply Matrix Transformation
In This Topic
    Apply Matrix Transformation
    In This Topic

    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..

    GcImaging supports graphics transformation through Transform property of the GcBitmapGraphics 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

    To apply matrix transformation:

    1. Initialize the GcBitmap class.
    2. Create a drawing surface using CreateGraphics method of the GcBitmap class which returns an instance of the GcBitmapGraphics class.
    3. Draw a rectangle using DrawRectangle method and apply the background color using FillRectangle method of the GcBitmapGraphics class.
    4. Define the text to be rendered in a rectangle.
    5. Add text to the rectangles using DrawString 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.
      C#
      Copy Code
      //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");
      
    Back to Top

    For more information about using transformation matrix in GcImaging, see GcImaging sample browser.