How to fit the image to to border after rotation in WPF

自古美人都是妖i 提交于 2020-06-26 14:43:19

问题


I have one border inside which am having an image. on button click am rotating that image to 90 degrees. this is my original image

The below is after rotation

As you can after rotation my image doesn't fit into the border. I need it to be fill the border completely. What is am missing here?


回答1:


I think, you are using RenderTransform to rotate the image.

Instead, use LayoutTransform.

See the sample:

<StackPanel>
    <Border Width="500" Height="300" BorderBrush="Black" BorderThickness="1">
        <Image Source="sombrero.jpg" Stretch="Fill" x:Name="img" HorizontalAlignment="Center" VerticalAlignment="Center">
        </Image>
    </Border>
    
    <Button Content="Rotate" Click="ButtonBase_OnClick"></Button>
</StackPanel>

Codebehind:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        img.LayoutTransform = new RotateTransform(90);
    }

Before:

After:

Hope this helps.



来源:https://stackoverflow.com/questions/43911072/how-to-fit-the-image-to-to-border-after-rotation-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!