2011-10-07 26 views
1

我在資源文件夾(比如,資源/位圖)的子文件夾中的一些位圖,我想它們加載到一個位圖[]在同一時間,也就是如何使用C#爲資源文件夾中的多個文件進行批量導入?

BitmapSource[] bitmaps= 
    TheMethodIWantToImplement(
    new Uri("pack://application:,,,/MyAssembly;component/Resources/Bitmap", 
    UriKind.Absolute)); 

但是我發現, Directory.GetFiles不接受Uri參數。所以我該怎麼做?謝謝。

+1

也許你可以在這個問題的答案中找到一些幫助:http://stackoverflow.com/questions/2517407/enumerating-net-assembly-resources-at-runtime – Teudimundo

回答

0

謝謝@Teudimundo。我修改該鏈接的片段如下:

var asm = Assembly.GetEntryAssembly(); 
string resName = asm.GetName().Name + ".g.resources"; 
using (var stream = asm.GetManifestResourceStream(resName)) 
using (var reader = new System.Resources.ResourceReader(stream)) 
{ 
    var paths = from entry in reader.Cast<DictionaryEntry>() 
       where ((string)entry.Key).Contains("sub-folder-name") 
       select entry.Key; 
    if (paths.Any()) 
    { 
     foreach (string path in paths) 
     { 
      Uri uri = new Uri(String.Format(
         "pack://application:,,,/{0};component{1}", asm.GetName(), path) 
         , UriKind.Absolute); 
      //use the uri for the rest 
     } 
    } 
} 

但我仍然需要使用直接(可通過select entry.Value在上面的LINQ表達式檢索)的資源流,這是UnmanagedMemoryStream一個實例。我需要MemoryStream來構建我的對象,但我沒有發現任何優雅的轉換方法。

相關問題