Basic Understanding of Matrix Transformation


Left to my own devices and not having good clear documentation this is my observation on how it works, but as it is just an informed guess I may be inaccurate.  This is targeted at people that are just starting to learn WPF

<Grid>

<Image Source="C:\Users\jofultz\Pictures\8fold.png" Width="320" Height="240" >

<Image.RenderTransform >

<MatrixTransform x:Name="myMatrixTransform">

<MatrixTransform.Matrix>

<Matrix OffsetX="0" OffsetY="0" M11="1" M12="0" />

</MatrixTransform.Matrix>

</MatrixTransform>

</Image.RenderTransform>

</Image>

</Grid>

The above XAML produces a standard normally oriented image for me to view. That looks something like this:

clip_image001

Indeed without the MatrixTransform I would have gotten the same result. The top left corner represents 0,0 and the bottom right represents 320,240. There has been no adjustment to the image and as such it renders in the default way. To understand the MatrixTransform let's pick it apart piece by piece. First, let's take the simplest modification and modify the offsets for X and Y. We'll change the Matrix element to read:

<Matrix OffsetX="20" OffsetY="40" M11="1" M12="0" />

So, it will shift the start point for rendering by the amount specified which will give us the following result:

clip_image002

Take note of the horizontal and vertical shift. However, the image orientation is the same as there has been no shift in the value or direction of the X and Y coordinates. To do this, we set values for M11 and M12 where M11 will be the multiplier for the starting X coordinate and M12 is the multiplier for the bottom left coordinate. Let's revert the image offset to 0's and flip it on its X axis. What does that mean in terms of M11? Since I simply want to flip it, but make no other change I will set the value to -1 as M11 represents the for coordinate pair of the object and a negative number will make it work in the opposite direction:

<Matrix OffsetX="0" OffsetY="0" M11="-1" M12="0" />

This changes the value of X from 320 to -320 resulting in an image that looks like:

clip_image003

I'd really like to get that image into the view window. Since it is now rendering to the left thanks to a sign change I need the final point to end up at 0 on the X axis. As such, I simply shift the OffsetX over the with by setting it to 320.

<Matrix OffsetX="320" OffsetY="0" M11="-1" M12="0" />

However, since my image has some blank space I'm actually going to set the OffsetX to 240 in order to ensure that it shows properly in the window.

Thus, the image will start rendering backward at 320 and end up at 0 resulting in this:

clip_image004

As you can see, I have now caused it to render backward in the same orientation as before by simply negating the X coordinates and then moved it into proper view with an OffsetX adjustment. Of note, if you want to stretch the image/control along the left side you need to set M12 to some other value as M12 represents the mdifier endpoint of the line going from top left to bottom left.

Lastly, let's do the same simple demo with the Y coordinate. If we want to flip it, but not skew it in any way we simply need to negate the Y coordinates by setting M22 equal to -1 as this will change the end point for the vertical rendering:

<Matrix OffsetX="320" OffsetY="0" M11="-1" M12="0" M22="-1" />

Which results in this image:

clip_image005

Once again we need to adjust the offset and since there is no empty space at the bottom or top this can be the same as the actual define window space:

<Matrix OffsetX="240" OffsetY="240" M11="-1" M12="0" M22="-1" />

This results in:

clip_image006

And that concludes my simple, but hopefully enlightening tutorial. Please, if it helped you let me know and I'll do more remedial getting to know your platform stuff like this based on what I wish someone would have told me. Additionally, if I misstated how something works, please, please, post a comment clarifying as the knowledge will only help me 🙂


Comments (1)

  1. Anders says:

    Thanks. I know matrixes can be used for scaling, rotation and so on but seeing it visualized is still helpful, if just to get the facts trough the skull.

Skip to main content