图像以适合WPF中的屏幕大小

时间:2017-12-27 10:28:13

标签: c# wpf xaml

我正在尝试使用保留的宽高比使图像拉伸以适应屏幕。我认为这段代码可以解决问题:

<Image Name="Viewer" Height="{Binding SystemParameters.PrimaryScreenHeight}"  Width="{Binding SystemParameters.PrimaryScreenHeight}"  Stretch="Uniform" StretchDirection="Both"/>

令人遗憾的是,图像似乎恰好适合屏幕的宽度并将其裁剪在底部。

完整的xaml代码如下所示:

enter co<Window x:Class="ImageExplorer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ImageExplorer"
    mc:Ignorable="d"
    Title="MainWindow" WindowState="Maximized" Background="Black">
<StackPanel>
    <Border BorderBrush="Black" BorderThickness="2">
        <Grid Margin="0,0,0,0">
            <Button Content="open" Click="OpenImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="1,1,1,1" />
            <Button Content="Prev." Click="GetPreviousImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="120,1,1,1"/>
            <Button Content="Next" Click="GetNextImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="180,1,1,1" />
            <Label Content="Fil:" HorizontalAlignment="Right" VerticalAlignment="Center" Width="120" Height="30" Margin="1,1,300,1"/>
            <TextBlock Name="StiViser" HorizontalAlignment="Right"  VerticalAlignment="Center" Width="298" Margin="1,1,1,1" TextWrapping="Wrap" Text="TextBlock"/>
        </Grid>
    </Border>
    <Image Name="Viewer" Height="{Binding SystemParameters.PrimaryScreenHeight}"  Width="{Binding SystemParameters.PrimaryScreenWidth}"  Stretch="Uniform" StretchDirection="Both"/>
</StackPanel>

1 个答案:

答案 0 :(得分:1)

改用DockPanel

 <DockPanel LastChildFill="True">
    <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="2">
        <Grid Margin="0,0,0,0">
            <Button Content="open" Click="OpenImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="1,1,1,1" />
            <Button Content="Prev." Click="GetPreviousImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="120,1,1,1"/>
            <Button Content="Next" Click="GetNextImage" HorizontalAlignment="Left" VerticalAlignment="Center" Width="58" Height="30" Margin="180,1,1,1" />
            <Label Content="Fil:" HorizontalAlignment="Right" VerticalAlignment="Center" Width="120" Height="30" Margin="1,1,300,1"/>
            <TextBlock Name="StiViser" HorizontalAlignment="Right"  VerticalAlignment="Center" Width="298" Margin="1,1,1,1" TextWrapping="Wrap" Text="TextBlock"/>
        </Grid>
    </Border>
    <Image Name="Viewer" Height="{Binding SystemParameters.PrimaryScreenHeight}"  Width="{Binding SystemParameters.PrimaryScreenWidth}"  Stretch="Fill" StretchDirection="Both"/>
</DockPanel>