如何在 WPF / XAML 应用中让图片拉伸以填满空间?

9
当我的程序显示的图像比在XAML中定义的Image GUI对象小时,它不会被拉伸以适应我想要的大小。例如,一个256x256的图像仅占据了512x512 Image GUI对象的左上象限。我很困惑,因为我已经在XAML代码中设置了Stretch="Fill"。我还需要做什么?非常感谢您提供代码帮助。
下面是定义GUI的XAML代码:
  <Window x:Class="MyProgram.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyProgarm" Height="900" Width="890" Name="mainWindow" Icon="Icon.ico" MouseDown="mouseDown">
        <Grid Name="mainGrid" Background="#FFEBE7F0" Width="800.10">
          <Border Margin="139,32,0,0" Name="border1" Height="512" VerticalAlignment="Top" HorizontalAlignment="Left" Width="512" Background="#F0D4D0D0" />
          <Border Margin="138,602,0,0" Name="border2" Height="256" VerticalAlignment="Top" HorizontalAlignment="Left" Width="256" Background="#F0D4D0D0" />
          <Border Margin="400,602,0,0" Name="border3" Height="256" VerticalAlignment="Top" HorizontalAlignment="Left" Width="256" Background="#F0D4D0D0" />
          <Image Margin="135,32,0,0" Name="imagePanel1" Stretch="Fill" HorizontalAlignment="Left" Width="512" MouseMove="imagePanelAxl_MouseMove" Height="512" VerticalAlignment="Top" Canvas.Left="-135" Canvas.Top="-32">
          </Image>

我用来绘制图片的代码:

byte[] myColorImage=// 256x256 RGB image loaded from disk

int W=256;
int H=256;  // dimensions of myColorImage
// Use multiple cores
image.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action<byte[]>(
      delegate(byte[] myColorImage_IN)
      {
          const int dpi = 96;

          BitmapSource bmpsrc = BitmapSource.Create(W, H, dpi, dpi, PixelFormats.Bgr24, null, myColorImage_IN, W * 3);
          image.Source = bmpsrc;


      }
  ), myColorImage);
4个回答

13

不要使用Image,而是使用ImageBrush,它可以为您完成工作。

 <Border.Background>
            <ImageBrush x:Name="image" Stretch="UniformToFill"/>
 </Border.Background>

在代码后台,你可以设置

   image.ImageSource = bmpsrc; // if you can give the URL of the File Located on the Disk

似乎名称不是ImageBrush的属性。如果是这样,是否可以通过编程方式分配图像? - myjy012003 a
1
x:Name 应该是我的错误。 - Nivid Dholakia

3

如上所述,这里是使用ImageBrush的代码。同时也在鼠标悬停时设置了图片。 希望能帮助新用户。

<Grid.Resources>
    <ImageBrush x:Key="AddButtonImageBrush" ImageSource="/DemoApp;component/Resources/AddButton.png" Stretch="Uniform"/>

    <Style x:Key="AddButtonImageStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{StaticResource AddButtonImageBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background"  Value="{StaticResource AddButtonImageBrush}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Grid.Resources>

<Button Content="If Text is also Required" Style="{StaticResource AddButtonImageStyle}"/>

1
如果您知道磁盘上图像的URI,您应该使用ImageBrush控件。

0

关于你的代码,我有几个评论:

而在你的XAML中,你给你的图片命名为:

imagePanel1

而在代码后台,您使用了:

image

在 XAML 中的图像标签中,您说:
Canvas.Left="-135" Canvas.Top="-32"

这个图像标签不在画布中,而是在网格中。


在代码后台中,您使用调度程序使事物使用多个核心吗?
调度程序用于将其他线程中的事物执行到UI线程中。如果图像(从中使用调度程序)也在UI线程中(我猜它是因为它是一个UI对象),那么这不会使您的应用程序使用多个核心。
如果您将图像边距设置为0并删除水平和垂直对齐。同时删除宽度和高度。那么您的图像应该完全填充窗口。
你能为我测试一下吗?

您可以始终使用Uri将BitmapSource设置为系统上的文件。

BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative); //instead you can also use Urikind.Absolute
bi3.EndInit();
imagePanel1.Stretch = Stretch.Fill;
imagePanel1.Source = bi3;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接