2012-06-15 31 views
3

我讀過很多示例。而我所擁有的似乎是正確的。但是,當它加載失敗。無法獲取WPF中的png作品C#代碼

這裏是我的代碼:

LargeImage=new BitmapImage(new Uri("pack://application:,,,/Images/books_48.png")) 

這段代碼在運行AssemblyA。 AssemblyA的項目中還有一個名爲Images的文件夾。該文件夾有一個名爲books_48.png的文件。它被設置爲「資源」編譯並且從不復制。

我用DotPeek來查看圖像是否在AssemblyA.dll中,並且它在那裏。

第一次參考LargeImage在AssemblyB中。它將LargeImage綁定到FluentRibbon Fluent:Button.LargeIcon。

當談到時間來加載的BitmapImage我得到這個錯誤:

Cannot locate resource 'images/books_48.png'.

如何得到這個加載任何想法?

注:我也曾嘗試這些:

"pack://application:,,,/AssemblyA;component/Images/books_48.png" 
"pack://application:,,,/AssemblyA;Images/books_48.png" 
"pack://application:,,,/AssemblyA;/Images/books_48.png" 
"pack://application:,,,/Images/books_48.png" 
"pack://application:,,,Images/books_48.png" 
"Images/books_48.png" 
"/Images/books_48.png" 

他們全都給我的錯誤(或者「找不到」或「無效的URI」之類的錯誤)。

+0

不應該是嵌入式資源嗎? –

+0

@PaulSullivan - 在對此答案的評論(http:// stackoverflow。com/a/483891/16241),人們似乎認爲嵌入式資源在99%的時間內是錯誤的。關於在應用程序中包含圖像兩次。 – Vaccano

+0

是的,就我所知,它應該只是資源。 Vaccano - 做大會B參考大會A?或者,他們都加載到另一個可執行文件...或? – Tim

回答

-2

查看Nuno Rodriguez對WPF image resources的回答。它解釋說,該路徑應該是:

「/«組件A»;組件/«YourPath»/«YourImage.png»」

+0

我試過,但因爲我在C#中(而不是在XAML中),那種路徑返回「無效的URI」錯誤(UriFormatException)。 – Vaccano

0

我用我的ImageUtilities類來獲得在C#中的圖像。我會附上下面的代碼..

我總是把我的圖像設置爲'資源''不要複製'。 然後我總是將我的所有圖像複製到我的Properties \ Resources.resx文件中。這可以在Visual Studio中通過展開作爲Project文件夾中第一項的'Properties'文件夾來完成。然後雙擊'Resources.resx'文件,設計器將彈出。在此窗口的左上角,點擊下拉菜單並選擇「圖片」。然後突出顯示項目中的所有圖像文件(從解決方案資源管理器中),並將它們複製/粘貼到Resources.resx的窗口中。

這樣,在後面的代碼,我可以通過鍵入訪問它們:

Properties.Resources.ImageName;

給你一個位圖。

我使用我的ImageUtilities類將其轉換爲與WPF一起使用的ImageSource。所以,如果我有所謂的「MyPicture.png」的圖像,我會做我的ImageUtilities級以下電話:

ImageSource source = ImageUtilities.ImageSourceFromResourceKey(
            Properties.Resources.MyPicture, "MyPicture"); 

我通過文本「MyPicture」究其原因,這樣我就可以緩存圖像,而不必每個圖像創建一次以上。

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Windows; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 

namespace Helpers 
{ 
    public static class ImageUtilities 
    { 
     [System.Runtime.InteropServices.DllImport("gdi32.dll")] 
     public static extern bool DeleteObject(IntPtr hObject); 
     public static Dictionary<String, ImageSource> _cachedImages = new Dictionary<string, ImageSource>(); 

     public static ImageSource ImageSourceFromBitmap(Bitmap bitmap) 
     { 
      if (bitmap == null) 
      { 
       return ImageSourceFromResourceKey(Properties.Resources.Exit, "Exit"); 
      } 

      try 
      { 
       IntPtr hBitmap = bitmap.GetHbitmap(); 
       BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
        hBitmap, 
        IntPtr.Zero, 
        Int32Rect.Empty, 
        BitmapSizeOptions.FromEmptyOptions()); 

       DeleteObject(hBitmap); 
       return bitmapSource; 
      } 
      catch (Exception ex) 
      { 
       //Error loading image.. Just log it... 
       Helpers.Debug(ex.ToString()); 
      } 

      return null; 
     } 

     public static ImageSource ImageSourceFromResourceKey(Bitmap bitmap, string imageKey) 
     { 
      if (!_cachedImages.ContainsKey(imageKey)) 
      { 
       _cachedImages[imageKey] = ImageSourceFromBitmap(bitmap); 
      } 

      return _cachedImages[imageKey]; 
     } 
    } 
}