对于我当前的项目,我有一个 IEnumerable 属性 ImageCollection 如下,
public static IEnumerable<Uri> ImageCollection { get; set; }
其中包含可供用户选择的所有应用内照片的集合。该功能包括用户可以从图库中进行选择。我正在使用 PhotoChooserTask 和下面的代码来使用下面的代码设置图像源,
private void photoChooserTask_Completed(object sender, PhotoResult e)
{
try
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage image = new BitmapImage();
image.SetSource(e.ChosenPhoto);
LibrImage.Source = image;
ViewModelLocator.SelectedImage = image;
}
}
catch (Exception)
{
Common.ShowMessageBox("Error occured while saving pic.");
}
}
但是,我想将引用 Uri 或 e.OriginalFileName 存储在数据库中。我了解到,即使将副本存储在独立存储中,我也无法实现我想要的 - 获取对使用 photoChooserTask 选择的文件的引用。我可能错了。
即使我最初的想法是将库文件的 Uri 添加到我的 ImageCollection 并将索引存储在每个项目的数据库中,任何建议或即兴创作都将受到赞赏。
注意:添加了CameraCaptureTask标签,因为PhotoChooserTask标签不可用,基本逻辑相同。
编辑代码用于存储和检索隔离存储
我将 GetImageFromIsolatedStorage() 方法的结果绑定到我的图像控制源。这不起作用..我做错了什么...
public static string SaveImageToIsolatedStorage(BitmapImage bitImg)
{
string fname = null;
if (bitImg != null)
{
fname = GetImageName();
WriteableBitmap wbmp = new WriteableBitmap(bitImg);
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
using (isf)
{
if (isf.FileExists(fname)) { isf.DeleteFile(fname); }
using (var stream = isf.OpenFile(fname, System.IO.FileMode.OpenOrCreate))
{
wbmp.SaveJpeg(stream, bitImg.PixelWidth, bitImg.PixelHeight, 0, 100);
stream.Close();
}
}
}
return fname;
}
private static string GetImageName()
{
return string.Format(
"{0}/GalleryImage{1}.jpg",
FileDir,
ImageCount++);
}
public static BitmapImage GetImageFromIsolatedStorage(string fname)
{
BitmapImage img = new BitmapImage();
try
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = isf.OpenFile(fname, FileMode.Open, FileAccess.Read))
{
img.SetSource(fileStream);
fileStream.Close();
}
}
}
catch { }
return img;
}