image transformation in C#

To perform image transformation in C# (c sharp), we'll need to use GDI+ of windows.
GDI+, supersedes GDI, is an improved 2D graphics environment. Graphics Device Interface (GDI) is one of the three core components or "subsystems" of Microsoft Windows. However, for games that need fast graphics rendering and rasterization for 3D, DirectX or OpenGL is preferred.

To use GDI+, you need to use System.Drawing namespace in C#.

There are several online tutorials on image transformation in C# using .NET GDI+.

MSDN
c-sharpcorner

codeproject

Several commonly used image transformation includes:

  • Rotation
  • Scaling
  • translate
  • Shearing
  • Reflection

Matrix transformation is at the heart of 2D graphics processing.
C#'s Matrix class, represents a 3 ×3 matrix that, in turn, represents an affine transformation.
An affine transformation consists of a linear transformation followed by a translation. Any linear representation, represented by 2 x 2 matrix, can be represented by a general affine transformation matrix.

Besides some linear algebra, some understanding of GDI+'s coordinate systems is also necessary for writing image transformation code.

For example, to rotate string text:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
public void draw(Graphics g)
{
Matrix mm=new Matrix();
//Bring Center to 200,200
mm.Translate(200, 200, MatrixOrder.Append);
//Apply world transformation
g.Transform = mm;
g.RotateTransform(90); // rotate clockwise 90 degree
g.DrawString("hello word", new Font("Verdana", 16), Brushes.Red,0,0);
}


Back to top