WPF旋转图像并对齐 [英] WPF Rotate an Image and align it
问题描述
我有一个Image组件,我想在其中旋转源:
I've an Image component where I want to rotate the source :
<Image Name="ImageTarget" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Uniform" RenderTransformOrigin=".5,.5">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Main.BindedViewMode, Converter={StaticResource ImageSizeConverter}}" />
<ScaleTransform ScaleY="{Binding Main.BindedViewMode, Converter={StaticResource ImageSizeConverter}}" />
<RotateTransform Angle="-90" />
</TransformGroup>
</Image.RenderTransform>
</Image>
我从代码中设置了ImageTarget
的来源.
I set the source of ImageTarget
from the code.
在进行转换之前(RenderTransformOrigin和RotateTransform),我的窗口就像:
Before the transformation (RenderTransformOrigin and RotateTransform) my window was like :
但是轮换之后:
因此,如您所见,宽度和高度已更改.
So, as you can see, the Width and Height has changed.
所以我的问题是:
- 为什么尺寸改变了?
- 如何在左上角对齐旋转的图像(与之前相同)?
谢谢
大小没有改变,我截取了两个不同大小的屏幕截图,然后stackoverflow自动调整了它们的大小.
Size hasn't changed, I have taken the two different screenshots with a different size, and stackoverflow automatically resize them.
推荐答案
问题是在布局传递之后应用了变换".在计算布局之前,应使用LayoutTransform
执行转换:
The problem is that the Transforms were applied after the layout pass. You should use a LayoutTransform
to perform the transformation before the layout is calculated:
<Image Name="ImageTarget" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Uniform" RenderTransformOrigin=".5,.5">
<Image.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleX="{Binding Main.BindedViewMode, Converter={StaticResource ImageSizeConverter}}" />
<ScaleTransform ScaleY="{Binding Main.BindedViewMode, Converter={StaticResource ImageSizeConverter}}" />
<RotateTransform Angle="-90" />
</TransformGroup>
</Image.LayoutTransform>
这篇关于WPF旋转图像并对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!