在我的WPF应用程序,我想旋转被放置在一个网格单元的图像:在旋转和自动缩放图片
- 使用
RotateTransform
直接:<Grid> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition/> </Grid.RowDefinitions> <Button Name="btnRotate" Click="btnRotate_Click">Rotate</Button> <Image Grid.Row="1" Name="img" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" /> </Grid>
我尝试了两种思路
Image
集装箱。不幸的是,这种转换取消了容器的属性VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
,并且图像超出了单元格边界。 - 在图像的
TransformedBitmap
实例上使用RotateTransform
。这个构造旋转和自动缩放,但GC不收集旧的实例;所以它浪费了很多RAM功能。
这是我的第二个想法背后的代码。如果它不会浪费那么多RAM,那就没问题。
private void btnRotate_Click(object sender, RoutedEventArgs e)
{
bmp = new TransformedBitmap(bmp, new RotateTransform(90));
bmp.Freeze();
img.Source = bmp;
}
那么,在其布局容器中使用自动缩放旋转图像的最佳做法是什么?
你有没有用尽全力RenderTransform和LayoutTransform? – CShark
我只使用RenderTransform。 – User95
在WPF已经计算出所有尺寸和尺寸之后,RenderTransform会被应用。 LayoutTransform更改对象,然后根据更改计算大小。应该值得一试。 – CShark