views:

113

answers:

3

I have the need to convert from the WPF Media.Matrix to the Windows Forms Drawing2D.Matrix and so I did the following:

  public static System.Drawing.Drawing2D.Matrix ConvertToDrawing2DMatrix( Matrix matrix)
  {
     return new System.Drawing.Drawing2D.Matrix((float)matrix.M11, 
                                                (float)matrix.M12, 
                                                (float)matrix.M21, 
                                                (float)matrix.M22,
                                                (float)matrix.OffsetX, 
                                                (float)matrix.OffsetY);
  }

and was wondering if this was the best approach.

A: 

If your code works fine then I would say that probably is the best method in your case. I looked all over google trying to find a way to do this and other than a 3rd party library I couldn't seem to find a way.

Lucas McCoy
A: 

I'd say that's the best way. The System.Drawing matrix is stored in unmanaged memory and the WPF matrix is a struct on the managed stack, so any trickery in doing a block copy would require some unsafe code for very little, if any, perf improvement.

codekaizen
A: 

As others have said that is probably the best way. I just wanted to add that depending on the .NET version and your coding policy you could consider adding "this" to the method signature and make it an extension method for easier access, like this:

using Drawing2DMatrix = System.Drawing.Drawing2D.Matrix;
public static Drawing2DMatrix ConvertToDrawing2DMatrix(this Matrix matrix) {...}

Then you could call it like this:

Drawing2DMatrix newMatrix = myMediaMatrixInstance.ConvertToDrawing2DMatrix();

Just a suggestion.

JohannesH