您可以先将图片保存在 IsolatedStorage 中,将文件路径作为字符串参数传递到另一个页面,在需要时将图片加载出来。
使用 PhoneApplicationService 将图像保存到 State 中,在需要时加载它。
保存到 IsolatedStorage 的示例:
public static void SaveStreamToStorage(Stream imgStream, string fileName)
{
using (IsolatedStorageFile iso_storage = IsolatedStorageFile.GetUserStoreForApplication())
{
//Structure the path you want for your file.
string filePath = GetImageStorePathByFileName(fileName);
//Constants.S_STORE_PATH is the path I want to store my picture.
if (!iso_storage.DirectoryExists(Constants.S_STORE_PATH))
{
iso_storage.CreateDirectory(Constants.S_STORE_PATH);
}
//I skip the process when I find the same file.
if (iso_storage.FileExists(filePath))
{
return;
}
try
{
if (imgStream.Length > 0)
{
using (IsolatedStorageFileStream isostream = iso_storage.CreateFile(filePath))
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(imgStream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 100);
isostream.Close();
bitmap.UriSource = null;
bitmap = null;
wb = null;
}
}
}
catch(Exception e)
{
if (iso_storage.FileExists(filePath))
iso_storage.DeleteFile(filePath);
throw e;
}
}
}
从 IsolatedStorage 读取图片的示例:
public static BitmapImage LoadImageFromIsolatedStorage(string imgName)
{
try
{
var bitmapImage = new BitmapImage();
//bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation;
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
//Check if file exists to prevent exception when trying to access the file.
if (!iso.FileExists(GetImageStorePathByFileName(imgName)))
{
return null;
}
//Since I store my picture under a folder structure, I wrote a method GetImageStorePathByFileName(string fileName) to get the correct path.
using (var stream = iso.OpenFile(GetImageStorePathByFileName(imgName), FileMode.Open, FileAccess.Read))
{
bitmapImage.SetSource(stream);
}
}
//Return the picture as a bitmapImage
return bitmapImage;
}
catch (Exception e)
{
// handle the exception
Debug.WriteLine(e.Message);
}
return null;
}