首页 > 解决方案 > 将图像缩放到适当的大小,但具有最大宽度/高度

问题描述

我们有一个图像需要显示为闪屏。除非宽度或高度超过某个最大值,否则该图像应以其实际大小显示。在这种情况下,应缩放图像以适合窗口。

我们目前拥有的工作正常,除非超过MaxWidth(or )。MaxHeight在这种情况下,它只是剪切图像而不是缩放它。

我发现了一个类似的问题,但它并不完全相同。有没有办法克服这个问题?

[XML]$Xaml = @"
<Window
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Window'
    WindowStartupLocation='CenterScreen' ResizeMode='NoResize'
    SizeToContent='WidthAndHeight' MaxWidth='600' MaxHeight='400'
    Title='Splash' WindowStyle='None'
>

<Grid>
    <!-- Use a grid with auto height and width to set the window to the size of the image -->
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <!-- Use a grid in the button to set the size of the counter and the pause/resume label -->
    <Image Source='$Path'/>
    <Button Name='TimerButton' VerticalAlignment='Top' HorizontalAlignment='Right' Margin='0,3,3,0'>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="15"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="6"/>
                <ColumnDefinition Width="26"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Grid.Row="0" Name='ResumePauseLabel' Padding='0' HorizontalContentAlignment='Left' Content='II'/>
            <Label Grid.Column="1" Grid.Row="0" Name='TimerLabel' Padding='0' HorizontalContentAlignment='Center' Content='$Seconds'/>
        </Grid>
    </Button>
    </Grid>
</Window>
"@

即使尝试这样做,图像仍然被切断:

<Image Source='$Path' Stretch='None' MaxHeight='400' MaxWidth='1800'/>

感谢@mm8 和@Clemens 的评论,最终解决方案是更改Grid了图像并设置了额外的属性:

        [XML]$Xaml = @"
<Window
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Window'
    WindowStartupLocation='CenterScreen' ResizeMode='NoResize'
    SizeToContent='WidthAndHeight' MaxWidth='1600 ' MaxHeight='1600'
>

<Grid>
        <Image Stretch="Uniform" StretchDirection="DownOnly"  Source='$Path'/>
    <Button Name='TimerButton' VerticalAlignment='Top' HorizontalAlignment='Right' Margin='0,3,3,0'>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="15"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="6"/>
                <ColumnDefinition Width="26"/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Grid.Row="0" Name='ResumePauseLabel' Padding='0' HorizontalContentAlignment='Left' Content='II'/>
            <Label Grid.Column="1" Grid.Row="0" Name='TimerLabel' Padding='0' HorizontalContentAlignment='Center' Content='$Seconds'/>
        </Grid>
    </Button>
    </Grid>
</Window>
"@

标签: wpfgrid

解决方案


您可以使用set to 进行Uniform拉伸。StretchDirectionDownOnly

Image 将被拉伸到其父 Panel 强加的布局边界,但永远不会大于其原始大小。

<Image Stretch="Uniform" StretchDirection="DownOnly" .../>

推荐阅读