在WPF中适合屏幕尺寸的图像



我正在尝试进行图像拉伸以适合屏幕,以使用保存纵横比。我认为此代码可以解决问题:

<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>

使用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>

最新更新