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 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.
To apply matrix transformation:
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"); |
For more information about using transformation matrix in DsImaging, see DsImaging sample browser.