This documentation is archived and is not being maintained.

Matrix.Multiply Method

Multiplies this Matrix object by the specified Matrix object by prepending the specified Matrix.

Overload List

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

[Visual Basic] Overloads Public Sub Multiply(Matrix)
[C#] public void Multiply(Matrix);
[C++] public: void Multiply(Matrix*);
[JScript] public function Multiply(Matrix);

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

[Visual Basic] Overloads Public Sub Multiply(Matrix, MatrixOrder)
[C#] public void Multiply(Matrix, MatrixOrder);
[C++] public: void Multiply(Matrix*, MatrixOrder);
[JScript] public function Multiply(Matrix, MatrixOrder);

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, an OnPaint event object. The code performs the following actions:

  • Creates three matrices.
  • Lists the contents of matrix 1 to the screen.
  • Multiplies matrix 1 by matrix 2 and stores the result in matrix 1.
  • Lists the contents of matrix 1 to the screen.
  • Multiplies the result stored in matrix 1 by matrix 3, and again stores the result in matrix 1.
  • Lists the contents of matrix 1 to the screen.
  • Draws a rectangle to the screen prior to applying the matrix 1 transform (the blue rectangle).
  • Applies the transform to the rectangle.
  • Draws the transformed rectangle to the screen (the red rectangle), using the same coordinates as the previous rectangle.

[Visual Basic, C#] Notice that the red rectangle has been scaled by a factor of two in the horizontal direction, then rotated 90 degrees, and then moved (translated) 250 points in the x direction and 50 points in the y direction.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of Multiply. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub MultiplyExample(e As PaintEventArgs)
Dim myPen As New Pen(Color.Blue, 1)
Dim myPen2 As New Pen(Color.Red, 1)
' Set up the matrices.
Dim myMatrix1 As New Matrix(2F, 0F, 0F, 1F, 0F, 0F)
' Scale.
Dim myMatrix2 As New Matrix(0F, 1F, - 1F, 0F, 0F, 0F)
' Rotate 90.
Dim myMatrix3 As New Matrix(1F, 0F, 0F, 1F, 250F, 50F)
' Translate.
' Display the elements of the starting matrix.
ListMatrixElementsHelper(e, myMatrix1, "Beginning Matrix", 6, 40)
' Multiply Matrix1 by Matrix 2.
myMatrix1.Multiply(myMatrix2, MatrixOrder.Append)
' Display the result of the multiplication of Matrix1 and
' Matrix2.
ListMatrixElementsHelper(e, myMatrix1, _
"Matrix After 1st Multiplication", 6, 60)
' Multiply the result from the pervious multiplication by
' Matrix3.
myMatrix1.Multiply(myMatrix3, MatrixOrder.Append)
' Display the result of the previous multiplication
' multiplied by Matrix3.
ListMatrixElementsHelper(e, myMatrix1, _
"Matrix After 2nd Multiplication", 6, 80)
' Draw the rectangle prior to transformation.
e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100)
e.Graphics.Transform = myMatrix1
' Draw the rectangle after transformation.
e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100)
End Sub
' A helper function to list the contents of a matrix.
Public Sub ListMatrixElementsHelper(e As PaintEventArgs, _
matrix As Matrix, matrixName As String, numElements As Integer, _
y As Integer)
' Set up variables for drawing the array
' of points to the screen.
Dim i As Integer
Dim x As Single = 20
Dim j As Single = 200
Dim myFont As New Font("Arial", 8)
Dim myBrush As New SolidBrush(Color.Black)
' Draw the matrix name to the screen.
e.Graphics.DrawString(matrixName + ":  ", myFont, myBrush, x, y)
' Draw the set of path points and types to the screen.
For i = 0 To numElements - 1
e.Graphics.DrawString(matrix.Elements(i).ToString() + ", ", _
myFont, myBrush, j, y)
j += 30
Next i
End Sub
        
[C#] 
public void MultiplyExample(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Blue, 1);
Pen myPen2 = new Pen(Color.Red, 1);
// Set up the matrices.
Matrix myMatrix1 = new Matrix(
2.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);  // Scale
Matrix myMatrix2 = new Matrix(
0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f); // Rotate 90,
Matrix myMatrix3 = new Matrix(
1.0f, 0.0f, 0.0f, 1.0f, 250.0f, 50.0f);  // Translate
// Display the elements of the starting matrix.
ListMatrixElements(e, myMatrix1, "Beginning Matrix", 6, 40);
// Multiply Matrix1 by Matrix 2.
myMatrix1.Multiply(myMatrix2, MatrixOrder.Append);
// Display the result of the multiplication of Matrix1 and
// Matrix2.
ListMatrixElements(e,
myMatrix1,
"Matrix After 1st Multiplication",
6,
60);
// Multiply the result from the pervious multiplication by
// Matrix3.
myMatrix1.Multiply(myMatrix3, MatrixOrder.Append);
// Display the result of the previous multiplication
// multiplied by Matrix3.
ListMatrixElements(e,
myMatrix1,
"Matrix After 2nd Multiplication",
6,
80);
// Draw the rectangle prior to transformation.
e.Graphics.DrawRectangle(myPen, 0, 0, 100, 100);
// Make the transformation.
e.Graphics.Transform = myMatrix1;
// Draw the rectangle after transformation.
e.Graphics.DrawRectangle(myPen2, 0, 0, 100, 100);
}
//-------------------------------------------------------
// The following function is a helper function to
// list the contents of a matrix.
//-------------------------------------------------------
public void ListMatrixElements(
PaintEventArgs e,
Matrix matrix,
string matrixName,
int numElements,
int y)
{
// Set up variables for drawing the array
// of points to the screen.
int i;
float x = 20, X = 200;
Font myFont = new Font("Arial", 8);
SolidBrush myBrush = new SolidBrush(Color.Black);
// Draw the matrix name to the screen.
e.Graphics.DrawString(
matrixName + ":  ",
myFont,
myBrush,
x,
y);
// Draw the set of path points and types to the screen.
for(i=0; i<numElements; i++)
{
e.Graphics.DrawString(
matrix.Elements[i].ToString() + ", ",
myFont,
myBrush,
X,
y);
X += 30;
}
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Matrix Class | Matrix Members | System.Drawing.Drawing2D Namespace

Show: