2011-11-02 23 views
4

当我的程序显示的图像小于XAML中定义的图像GUI对象时,它不会被拉伸到适合我喜欢的程度。 例如,256x256图像只占用我512x512图像GUI对象的左上象限。我很困惑,因为我在XAML代码中设置了Stretch =“Fill”。我还需要做些什么?任何帮助代码(见下文)将不胜感激。如何绘制图像以填充此WPF/XAML应用程序?

XAML代码定义GUI

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

回答

1

如果您在磁盘上已知的图像的URI,你应该useImageBrush控制。

7

相反图像使用图像画笔,将做的工作对你

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

似乎该名称不是ImageBrush的属性。如果是这样的话,是否可以通过编程方式分配图像? –

+1

x:将它命名应该是我的错误。 –

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

正如很多人在这里所说的是使用ImageBrush的代码。其中也设置图像,而MouseOver。 认为它可能有助于新用户。

<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}"/>