2012-09-11 41 views
7

我在Resource.resx中包含了一個圖標文件,我想在堆棧面板內的TreeViewItem上顯示。如何使用Resources.resx鏈接圖像

1)可以將.ico文件用於此目的嗎?或者它必須是.bmp或jpg?

2)您如何設置XAML中的源代碼?下面的代碼不適合我

<StackPanel Orientation="Horizontal"> 
    <Image Margin="2" Source="/Resources/Folder_Back.ico" /> 
    <TextBlock Margin="2" Text="{Binding ProgramName}" 
    Foreground="White" FontWeight="Bold"/> 
</StackPanel> 

回答

5

你不能這樣做。只有在的WinForms奏效

看到這個帖子更多信息

Different way how add image to resources

使用在這個崗位

WPF image resources

代替

報價所示的方法:

如果您將在多個位置使用圖像,那麼將圖像數據僅加載到內存中,然後在所有Image元素之間共享圖像數據是值得的。

要做到這一點,創建一個BitmapSource作爲地方資源:

<BitmapImage x:Key="MyImageSource" UriSource="../Media/Image.png" /> 

然後,在你的代碼,使用類似:

<Image Source="{StaticResource MyImageSource}" /> 

以我爲例,我發現,我不得不將Image.png文件設置爲具有Resource的構建操作,而不是Content。這會導致圖像被載入您的編譯程序集。

+0

我可以使用.ico文件作爲圖像源嗎? – l46kok

+0

是的。只需使用.ico而不是PNG。你甚至可以在VS – Nahum

12

下面是訪問資源文件映像一招:你需要添加引用到項目性質類似這樣的

Accessing image from Resource File in XAML markup

第一:

xmlns:properties="clr-namespace:MyProject.Properties" 

,然後通過XAML訪問它像這樣的:

<image source="{Binding Source={x:Static properties:Resources.ImageName}}" /> 

您可以使用PNG/JPG/BMP以及ICO文件,但每個人都推薦PNG。

+6

內進行編輯這不起作用。什麼都沒有出現。我試圖使用它作爲圖標以及圖像。 –

3

使Qorbani工作的解決方案添加一個轉換器到Image Source.Binding!

XAML - 命名空間

xmlns:properties="clr-namespace:YourNameSpace.Properties" 
xmlns:converter="clr-namespace:YourNameSpace.Converter" 

的Xaml - 資源(用戶控件或窗口)

<UserControl.Resources> 
     <ResourceDictionary> 
       <converter:BitmapToImageSourceConverter x:Key="BitmapToImageSourceConverter" /> 
     </ResourceDictionary> 
</UserControl.Resources> 

XAML代碼

<StackPanel Orientation="Horizontal"> 
        <Image Width="32" Height="32" Source="{Binding Source={x:Static properties:Resources.Import}, Converter={StaticResource BitmapToImageSourceConverter}}" Stretch="Fill" /> 
        <TextBlock Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center">Import</TextBlock> 
</StackPanel> 

BitmapToImageSourceConverter.cs

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Globalization; 
using System.Linq; 
using System.Text; 
using System.Windows.Data; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

namespace YourNameSpace 
{ 
    public class BitmapToImageSourceConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var bitmap = value as System.Drawing.Bitmap; 
      if (bitmap == null) 
       throw new ArgumentNullException("bitmap"); 

      var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); 

      var bitmapData = bitmap.LockBits(
       rect, 
       ImageLockMode.ReadWrite, 
       System.Drawing.Imaging.PixelFormat.Format32bppArgb); 

      try 
      { 
       var size = (rect.Width * rect.Height) * 4; 

       return BitmapSource.Create(
        bitmap.Width, 
        bitmap.Height, 
        bitmap.HorizontalResolution, 
        bitmap.VerticalResolution, 
        PixelFormats.Bgra32, 
        null, 
        bitmapData.Scan0, 
        size, 
        bitmapData.Stride); 
      } 
      finally 
      { 
       bitmap.UnlockBits(bitmapData); 
      } 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 
+0

爲什麼沒有更多upvotes?該解決方案完成這項工作。 – AndyUK