2014-01-28 90 views
1

我可以使用专门的JPEG加载jpeg方法加载JPEG,我还可以使用SO上详细介绍的许多方法保存PNG。从Windows Phone上的独立存储加载PNG

但是,每当我创建一个用于从独立存储装载PNG的流时,它都会生成一个零大小的BitmapImage。

这是我有...

public static async Task<BitmapImage> ReadBitmapImageFromIsolatedStorage(string fn) 
{ 

     StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; 

     if (local != null) 
     { 
      Stream file = await local.OpenStreamForReadAsync(fn); 
      BitmapImage bi = new BitmapImage(); 
      bi.SetSource(file); 
      return bi; 

     } 

     return null; 

} 

我已经尝试了许多变化形式。在创建BitmapImages时,看起来会有某种延迟来读取流,这意味着流在BitmapImage读取之前经常被丢弃。在WPF中有一个可以设置的选项,但是WIndows Phone BitmapImage没有这个选项。

+0

想到我没有厌倦它 - 但也许http://stackoverflow.com/questions/4817874/problem-opening-jpeg-from-isolated-storage-on-windows-phone-7将帮助你。在答案@Stuart从IS打开一个PNG文件。 – Romasz

+0

此解决方案非常棒! –

回答

0

这是我发现的作品到底使用新的API Windows.Storage

我不是100%清楚的问题是什么,但原来这工作。这有一些额外的功能,如果它无法读取,它会重试一秒钟左右...

public static async Task<BitmapImage> ReadBitmapImageFromIsolatedStorage(string fn) 
    { 
     Uri uri = new Uri(String.Format("ms-appdata:///local/{0}", fn)); 
     // FileAccessAttempts is just an int that determines how many time 
     // to try reading before giving up 
     for (int i = 0; i < AppConfig.FileAccessAttempts; i++) 
     { 
      try 
      { 
       StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri); 
       Stream stream = await file.OpenStreamForReadAsync(); 
       BitmapImage bi = new BitmapImage(); 
       bi.SetSource(stream); 
       return bi; 
      } 
      catch 
      { 
       // Similar functions also have a ~1 second retry interval so introduce 
       // a random element to prevent blocking from accidentally synched retries 
       Random r = new Random(); 
       System.Threading.Thread.Sleep(950 + (int)Math.Floor(r.Next() * 50.0)); 
      } 
     } 
     return new BitmapImage(); 
    } 
0

试试这个:

BitmapImage image = new BitmapImage(); 
image.DecodePixelWidth = 500; //desired width, optional 
image.DecodePixelHeight = 500; //desired height, optional 

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    if (myIsolatedStorage.FileExists("imagepath")) 
    { 
     using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("imagepath", FileMode.Open, FileAccess.Read)) 
     { 
      image.SetSource(fileStream); 
     } 
    } 
} 
+0

除非'DecodePixel [Width | Height]'有所作为,否则不起作用。只有在最近的几次尝试中,我才更改为新的“Windows.Storage”方法... – Brendan

相关问题