简体   繁体   English

XAML - 如何反转UIElement.RenderTransform?

[英]XAML - How to invert a UIElement.RenderTransform?

I am trying to add a watermark in a XOD file (XOD is very similar to XPS files as far as I know). 我试图在XOD文件中添加水印(据我所知,XOD与XPS文件非常相似)。 Inside XODs, there are XAML files with content like that: 在XOD内部,有一些XAML文件包含如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<Canvas RenderTransform="1.3333333,0,0,-1.3333333,0,990.236">
....
</Canvas>
</FixedPage>

I want to insert my watermark inside the Canvas, but I want to place it in the same page position for each document. 我想在Canvas中插入我的水印,但我想将它放在每个文档的相同页面位置。 The thing is that the RenderTransform 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 Canvas element into a new Canvas without RenderTransform but I don't really like this). 问题是RenderTransform正在从一个文档更改为文档,所以我应该计算逆变换来做到这一点(我还想过将旧的Canvas元素包装到一个没有RenderTransform的新Canvas ,但我真的不喜欢这个) 。

I have found some related documentation and questions for these: 我找到了一些相关的文档和问题:

http://msdn.microsoft.com/en-us/library/ms750596(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/ms750596(v=vs.110).aspx

Default RenderTransform Converter syntax 默认的RenderTransform转换器语法

 <Canvas RenderTransform="m11, m12, m21, m22, offsetX, offsetY">

 m11   m12    0           a  b  0
 m21   m22    0     or    c  d  0
 offx  offy   1           tx ty 1

...and I have calculated what I thought was the inverse matrix: ......我计算了我认为的逆矩阵:

        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;

...and multiplied it with my desired transform, p ([f] = [i] * [p]): ...并将其乘以我想要的变换,p([f] = [i] * [p]):

        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;

Then my element inside the Canvas would be: 那么我在Canvas元素将是:

<Glyphs RenderTransform="fa, fb, fc, fd, ftx, fty" .... />

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. 然而,M **系数似乎是正确的。 Am I missing something here? 我在这里错过了什么吗? Are the translation coefficients supposed to be calculated in another way? 翻译系数是否应该以另一种方式计算?

Using : 使用:

        ftx = ptx - tx;
        fty = pty + pageHeight - ty;

..seemed to provide similar results for most of the RenderTransforms, but not all. ..看起来为大多数RenderTransforms提供了类似的结果,但不是全部。

EDIT: I try to avoid using any of the WPF assemblies because I don't need any other functionality from there. 编辑:我尽量避免使用任何WPF程序集,因为我不需要任何其他功能。

if i understand you correctly and you just want to compute the inverse of a RenderTransform then why don't you use the Invert() function of the RenderTransforms underlying matrix? 如果我理解正确你只想计算RenderTransform的逆,那你为什么不使用RenderTransforms底层矩阵的Invert()函数?

Transform t = elem.RenderTransform;
if(t.Value.HasInverse) {
    t.Value = t.Value.Invert();
    elem.RenderTransform = t;
}

Where elem is your UIElement. elem是你的UIElement。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否由TransformToAncestor使用UIElement.RenderTransform? - Is UIElement.RenderTransform used by TransformToAncestor? 如何在执行RenderTransform(缩放和翻译)后获取UIElement的新位置和大小? - How can I get the new position and size of a UIElement after performing a RenderTransform (Scale & Translate)? 如何在WinRT XAML C#中克隆UIElement? - How to clone UIElement in WinRT XAML C#? 如何在WPF绑定中将UIElement或XAML作为内容返回 - How to return UIElement or XAML as content in WPF Binding 从XAML实例化UIElement - Instanciate UIElement from XAML Windows应用商店应用程序:使用RenderTransform时,UIElement无法完全呈现 - Windows Store app: UIElement not rendered completly when using RenderTransform 是否可以将所有受UIElement RenderTransform影响的属性复制到新元素中? - Can all of a UIElement RenderTransform effected properties be copied into a new Element? 如何在XAML C#Windows App中以编程方式更改控件的ScaleY RenderTransform? - How to programically change ScaleY RenderTransform of a control in a XAML C# Windows App? WinRT控件呈现XAML UIElement - WinRT control to render XAML UIElement 如何使用C#反转XAML PNG图像的颜色? - How to Invert Color of XAML PNG Images using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM