我可以使用专门的JPEG加载jpeg方法加载JPEG,我也可以使用SO上详述的许多方法保存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 :(得分: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);
}
}
}
答案 1 :(得分:0)
这是我最终使用新的Windows.Storage
API
我不是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();
}