title: XAML - How to invert a UIElement.RenderTransform?
body: <p>I am trying to add a watermark in a XOD file (XOD is very similar to XPS files as far as I know).
  Inside XODs, there are XAML files with content like that:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;FixedPage xmlns="http://schemas.microsoft.com/xps/2005/06" xmlns:x="http://schemas.microsoft.com/xps/2005/06/resourcedictionary-key" xml:lang="EN" Width="725.66667" Height="990.236"&gt;
  &lt;Canvas RenderTransform="1.3333333,0,0,-1.3333333,0,990.236"&gt;
  ....
  &lt;/Canvas&gt;
  &lt;/FixedPage&gt;
  </code></pre>
  
  <p>I want to insert my watermark inside the Canvas, but I want to place it in the same page position for each document. The thing is that the <code>RenderTransform</code> is changing from document to document, so I should probably calculate the inverse transform to do this (I have also thought of wrapping the old <code>Canvas</code> element into a new <code>Canvas</code> without <code>RenderTransform</code> but I don't really like this).</p>
  
  <p>I have found some related documentation and questions for these:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms750596(v=vs.110).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms750596(v=vs.110).aspx</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12359536/default-rendertransform-converter-syntax">Default RenderTransform Converter syntax</a></p>
  
  <pre><code> &lt;Canvas RenderTransform="m11, m12, m21, m22, offsetX, offsetY"&gt;
  
   m11   m12    0           a  b  0
   m21   m22    0     or    c  d  0
   offx  offy   1           tx ty 1
  </code></pre>
  
  <p>...and I have calculated what I thought was the inverse matrix:</p>
  
  <pre><code>        float det = a * d - b * c;
          float ia, ib, ic, id, itx, ity;
          ia = d / det;
          ib = -b / det;
          ic = -c / det;
          id = a / det;
          itx = (c * ty - d * tx) / det;
          ity = -(a * ty - b * tx) / det;
  </code></pre>
  
  <p>...and multiplied it with my desired transform, p ([f] = [i] * [p]):</p>
  
  <pre><code>        float pa = 60, pb = -60, pc = 60, pd = 60, ptx = 20, pty = 20;
          float fa, fb, fc, fd, ftx, fty;
          fa = ia * pa + ib * pc;
          fb = ia * pb + ib * pd;
          fc = ic * pa + id * pc;
          fd = ic * pb + id * pd;
          ftx = itx * pa + ity * pc + ptx;
          fty = itx * pb + ity * pd + pty;
  </code></pre>
  
  <p>Then my element inside the <code>Canvas</code> would be:</p>
  
  <pre><code>&lt;Glyphs RenderTransform="fa, fb, fc, fd, ftx, fty" .... /&gt;
  </code></pre>
  
  <p>But this doesn't seem to work for the Offset coefficients. They seem to get too big values and go outside the page. The M** coefficients seem to come out correct though.
  Am I missing something here?
  Are the translation coefficients supposed to be calculated in another way?</p>
  
  <p>Using :</p>
  
  <pre><code>        ftx = ptx - tx;
          fty = pty + pageHeight - ty;
  </code></pre>
  
  <p>..seemed to provide similar results for most of the RenderTransforms, but not all.</p>
  
  <p><strong>EDIT:</strong>
  I try to avoid using any of the WPF assemblies because I don't need any other functionality from there.</p>