wpf - 将图像缩放到适当的大小,但具有最大宽度/高度
问题描述
我们有一个图像需要显示为闪屏。除非宽度或高度超过某个最大值,否则该图像应以其实际大小显示。在这种情况下,应缩放图像以适合窗口。
我们目前拥有的工作正常,除非超过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>
"@
解决方案
您可以使用set to 进行Uniform
拉伸。StretchDirection
DownOnly
Image 将被拉伸到其父 Panel 强加的布局边界,但永远不会大于其原始大小。
<Image Stretch="Uniform" StretchDirection="DownOnly" .../>
推荐阅读
- python-3.x - Python解释器在接收输入后关闭
- python - 防止短信验证滥用
- sql - AlwaysOn 可用性组存储
- android - 解决 RecylcerView 中 toLowerCase() 空指针异常的最佳解决方案
- ios - 如何使preferredStatusBarStyle 成为一个函数
- android - 无法播放此视频
- python - 我如何证明和分析代码的运行时间,是 O(n) 吗?
- javascript - 将 Firebase 时间戳转换为当前时间或其他可用形式
- javascript - Square API,读卡器销售产品的监听器
- python - Python语法中的“comp_for”是什么意思?