Matrix Class

 

Encapsulates a 3-by-3 affine matrix that represents a geometric transform. This class cannot be inherited.

Namespace:   System.Drawing.Drawing2D
Assembly:  System.Drawing (in System.Drawing.dll)

System.Object
  System.MarshalByRefObject
    System.Drawing.Drawing2D.Matrix

public sealed class Matrix : MarshalByRefObject, IDisposable

NameDescription
System_CAPS_pubmethodMatrix()

Initializes a new instance of the Matrix class as the identity matrix.

System_CAPS_pubmethodMatrix(Rectangle, Point[])

Initializes a new instance of the Matrix class to the geometric transform defined by the specified rectangle and array of points.

System_CAPS_pubmethodMatrix(RectangleF, PointF[])

Initializes a new instance of the Matrix class to the geometric transform defined by the specified rectangle and array of points.

System_CAPS_pubmethodMatrix(Single, Single, Single, Single, Single, Single)

Initializes a new instance of the Matrix class with the specified elements.

NameDescription
System_CAPS_pubpropertyElements

Gets an array of floating-point values that represents the elements of this Matrix.

System_CAPS_pubpropertyIsIdentity

Gets a value indicating whether this Matrix is the identity matrix.

System_CAPS_pubpropertyIsInvertible

Gets a value indicating whether this Matrix is invertible.

System_CAPS_pubpropertyOffsetX

Gets the x translation value (the dx value, or the element in the third row and first column) of this Matrix.

System_CAPS_pubpropertyOffsetY

Gets the y translation value (the dy value, or the element in the third row and second column) of this Matrix.

NameDescription
System_CAPS_pubmethodClone()

Creates an exact copy of this Matrix.

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodDispose()

Releases all resources used by this Matrix.

System_CAPS_pubmethodEquals(Object)

Tests whether the specified object is a Matrix and is identical to this Matrix.(Overrides Object.Equals(Object).)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Overrides Object.Finalize().)

System_CAPS_pubmethodGetHashCode()

Returns a hash code.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodInvert()

Inverts this Matrix, if it is invertible.

System_CAPS_pubmethodMultiply(Matrix)

Multiplies this Matrix by the matrix specified in the matrix parameter, by prepending the specified Matrix.

System_CAPS_pubmethodMultiply(Matrix, MatrixOrder)

Multiplies this Matrix by the matrix specified in the matrix parameter, and in the order specified in the order parameter.

System_CAPS_pubmethodReset()

Resets this Matrix to have the elements of the identity matrix.

System_CAPS_pubmethodRotate(Single)

Prepend to this Matrix a clockwise rotation, around the origin and by the specified angle.

System_CAPS_pubmethodRotate(Single, MatrixOrder)

Applies a clockwise rotation of an amount specified in the angle parameter, around the origin (zero x and y coordinates) for this Matrix.

System_CAPS_pubmethodRotateAt(Single, PointF)

Applies a clockwise rotation to this Matrix around the point specified in the point parameter, and by prepending the rotation.

System_CAPS_pubmethodRotateAt(Single, PointF, MatrixOrder)

Applies a clockwise rotation about the specified point to this Matrix in the specified order.

System_CAPS_pubmethodScale(Single, Single)

Applies the specified scale vector to this Matrix by prepending the scale vector.

System_CAPS_pubmethodScale(Single, Single, MatrixOrder)

Applies the specified scale vector (scaleX and scaleY) to this Matrix using the specified order.

System_CAPS_pubmethodShear(Single, Single)

Applies the specified shear vector to this Matrix by prepending the shear transformation.

System_CAPS_pubmethodShear(Single, Single, MatrixOrder)

Applies the specified shear vector to this Matrix in the specified order.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodTransformPoints(Point[])

Applies the geometric transform represented by this Matrix to a specified array of points.

System_CAPS_pubmethodTransformPoints(PointF[])

Applies the geometric transform represented by this Matrix to a specified array of points.

System_CAPS_pubmethodTransformVectors(Point[])

Applies only the scale and rotate components of this Matrix to the specified array of points.

System_CAPS_pubmethodTransformVectors(PointF[])

Multiplies each vector in an array by the matrix. The translation elements of this matrix (third row) are ignored.

System_CAPS_pubmethodTranslate(Single, Single)

Applies the specified translation vector (offsetX and offsetY) to this Matrix by prepending the translation vector.

System_CAPS_pubmethodTranslate(Single, Single, MatrixOrder)

Applies the specified translation vector to this Matrix in the specified order.

System_CAPS_pubmethodVectorTransformPoints(Point[])

Multiplies each vector in an array by the matrix. The translation elements of this matrix (third row) are ignored.

In GDI+ you can store an affine transformation in a Matrix object. Because the third column of a matrix that represents an affine transformation is always (0, 0, 1), you specify only the six numbers in the first two columns when you construct a Matrix object. The statement Matrix myMatrix = new Matrix(0, 1, -1, 0, 3, 4) constructs the matrix shown in the following figure.

Transformations

A composite transformation is a sequence of transformations, one followed by the other. Consider the matrices and transformations in the following list:

Matrix A

Rotate 90 degrees

Matrix B

Scale by a factor of 2 in the x direction

Matrix C

Translate 3 units in the y direction

If we start with the point (2, 1) — represented by the matrix [2 1 1] — and multiply by A, then B, then C, the point (2, 1) will undergo the three transformations in the order listed.

[2 1 1]ABC = [-2 5 1]

Rather than store the three parts of the composite transformation in three separate matrices, you can multiply A, B, and C together to get a single 3×3 matrix that stores the entire composite transformation. Suppose ABC = D. Then a point multiplied by D gives the same result as a point multiplied by A, then B, then C.

[2 1 1]D = [-2 5 1]

The following illustration shows the matrices A, B, C, and D.

Transformations

The fact that the matrix of a composite transformation can be formed by multiplying the individual transformation matrices means that any sequence of affine transformations can be stored in a single Matrix object.

System_CAPS_cautionCaution

The order of a composite transformation is important. In general, rotate, then scale, then translate is not the same as scale, then rotate, then translate. Similarly, the order of matrix multiplication is important. In general, ABC is not the same as BAC.

The Matrix class provides several methods for building a composite transformation: Multiply, Rotate, RotateAt, Scale, Shear, and Translate. The following example creates the matrix of a composite transformation that first rotates 30 degrees, then scales by a factor of 2 in the y direction, and then translates 5 units in the x direction:

Matrix myMatrix = new Matrix();
myMatrix.Rotate(30);
myMatrix.Scale(1, 2, MatrixOrder.Append);
myMatrix.Translate(5, 0, MatrixOrder.Append);

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: