2011-11-02 26 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}"/>