Matrix Class (GDI+)
Overview
The Matrix class represents a 3×2 matrix used for geometric transformations in GDI+. It provides methods to translate, rotate, scale, and shear graphics objects.
Properties
Elements– Gets or sets the six floating‑point values of the matrix.IsIdentity– Indicates whether the matrix is the identity matrix.IsInvertible– Indicates whether the matrix can be inverted.
Methods
Translate(float dx, float dy, MatrixOrder order = MatrixOrder.Prepend)Rotate(float angle, MatrixOrder order = MatrixOrder.Prepend)Scale(float sx, float sy, MatrixOrder order = MatrixOrder.Prepend)Shear(float shearX, float shearY, MatrixOrder order = MatrixOrder.Prepend)Invert()TransformPoints(PointF[] pts)Multiply(Matrix matrix, MatrixOrder order = MatrixOrder.Prepend)
Examples
C#
C++
using System.Drawing;
using System.Drawing.Drawing2D;
Bitmap bmp = new Bitmap(300, 200);
using (Graphics g = Graphics.FromImage(bmp))
{
Matrix m = new Matrix();
m.Translate(150, 100);
m.Rotate(45);
g.Transform = m;
g.FillEllipse(Brushes.CornflowerBlue, -50, -50, 100, 100);
bmp.Save("rotated.png");
}
#include
using namespace Gdiplus;
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
Bitmap bitmap(300,200,PixelFormat32bppARGB);
Graphics* g = Graphics::FromImage(&bitmap);
Matrix m;
m.Translate(150,100);
m.Rotate(45);
g->SetTransform(&m);
g->FillEllipse(&SolidBrush(Color::Blue), -50,-50,100,100);
bitmap.Save(L"rotated_cpp.png", &EncoderPNG, nullptr);
delete g;
GdiplusShutdown(gdiplusToken);
return 0;
}
Remarks
When concatenating transformations, the order of operations matters. Use MatrixOrder.Prepend to apply a new transformation before the existing one, or MatrixOrder.Append to apply it after.