2013-02-14 69 views
3

我想顯示通過結合存儲在StorageFile圖片內容的圖片,但不管我試圖做到這一點似乎並沒有工作。顯示存儲在存儲文件中metroapp

這裏有兩種解決方案我已經測試:

string img =(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))).Path; 

然後通過結合返回到源物業所獲得的路徑(一個完整的絕對路徑文件),並且:

BitmapImage img = await LoadImage(await CompetencesFolder.GetFileAsync(FormatImageNameToFileName(imageName))); 

    private static async Task<BitmapImage> LoadImage(StorageFile file) 
     { 
      BitmapImage bitmapImage = new BitmapImage(); 
      FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read); 

      bitmapImage.SetSource(stream); 


      return bitmapImage; 

     } 

稍後將最終的bitmapImage返回到綁定屬性。

這些方法沒有工作..

千萬人有一個想法?

編輯:FIX

下面是解決該問題的代碼:

BitmapImage img = new BitmapImage() { UriSource = new Uri(LOCAL_REPOSITORY.Path + "/Assets/gfx/cards/" + FormatImageNameToFileName(imageName) + ".jpg", UriKind.RelativeOrAbsolute) }; 

我做了2個樣品的混合上面:我創造了我的BitmapImage從圖片的絕對URI (LOCAL_REPOSITORY包含對本地存儲的引用:ApplicationData.Current.LocalFolder

我仍然無法確定爲什麼其他兩種方式失敗:我通常將圖像直接綁定到Uri,字符串或BitmapImage的,和它的作品..

+0

你什麼錯誤? – 2013-02-14 21:25:06

+0

我沒有收到任何信息:圖片沒有被顯示,沒有發生崩潰,也沒有提出錯誤。 我解決了這個問題(請參閱編輯),但我仍然無法確定爲什麼其他兩個樣本不起作用。 – 2013-02-14 21:33:26

+0

圖片是否位於其他樣本的本地文件夾中?商店應用程序沒有對文件系統的任意訪問權限。 – 2013-02-14 21:40:11

回答

8

下面的代碼演示瞭如何使用的LoadImage方法中的應用:創建一個空白的應用程序,添加圖像和按鈕的主網頁。

private async void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     // load file from document library. Note: select document library in capabilities and declare .png file type 
     string filename = "Logo.png"; 
     Windows.Storage.StorageFile sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename); 
     // load file from a local folder 
     //Windows.Storage.StorageFile sampleFile = sampleFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("Assets\\Logo.png"); 

     BitmapImage img = new BitmapImage(); 
     img = await LoadImage(sampleFile); 
     myImage.Source = img; 
    } 

    private static async Task<BitmapImage> LoadImage(StorageFile file) 
    { 
     BitmapImage bitmapImage = new BitmapImage(); 
     FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read); 

     bitmapImage.SetSource(stream); 

     return bitmapImage; 

    } 
+1

加空指針異常的檢查(例如:file.OpenAsync中的LoadImage) – 2015-09-16 09:36:40

+0

你好的人,一個問題...是更多鈔票base64轉換字符串StorageFile?我想附加一個base64生成的文件...請參閱當前的代碼http://screencast.com/t/N0Sr2G3nJ – jdnichollsc 2016-01-08 22:10:15