2015-07-10 43 views
0

我一直在努力工作幾天,試圖找出如何保存和加載和從孤立存儲加載圖像。昨天,我終於設法解決了存儲它們時遇到的任何問題,但現在我需要將圖像作爲圖標添加到菜單項中,並且我不知道我的代碼有什麼問題:顯示來自孤立存儲的PNG圖像

var image = new System.Windows.Controls.Image(); 
using(var stream = new IsolatedStorageFileStream((string) (directory + file + ext), 
        FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly())) 
{ 
    image.Source = (BitmapSource) new PngBitmapDecoder(stream, 
        BitmapCreateOptions.PreservePixelFormat, 
        BitmapCacheOptions.Default).Frames[0]; 
} 

Menu menu = new Menu(); 
MenuItem item = new MenuItem(); 
item.Header = file; 
item.Icon = image; 
menu.Items.Add(item); 

圖像以合適的尺寸出現在菜單中,但是它是空白圖像。在Windows照片查看器中預覽圖像時,圖像文件顯示圖像很好。我對C#和WPF(僅與它一起工作了3個月)還很新,我正在尋找一個簡單的解決方案,它並不需要優雅或通用;它只是需要工作。

回答

0

它表明我會在求助後半小時回答我自己的問題。在網上搜索了好幾天,試圖找出它,它只是落在我的腿上。我只是需要用正確的FileAccess實例我流:

我剛剛更換

using(var stream = new IsolatedStorageFileStream((string) (directory + file + ext), 
        FileMode.Open, IsolatedStorageFile.GetUserStoreForAssembly())) 

using(var stream = IsolatedStorageFile.GetUserStoreForAssembly().OpenFile(
        (string) (directory + file + ext), FileMode.Open, FileAccess.Read))