For my current project, i have an IEnumerable property ImageCollection as follow,
public static IEnumerable<Uri> ImageCollection { get; set; }
Which holds a collection of all in-app photos available for user to choose. The feature includes user be able to choose from Gallery. I am using PhotoChooserTask and below code to set the Image Source using below code,
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.");
}
}
However, i would want to store the reference, Uri or e.OriginalFileName, in database. I learnt that even if a store the copy in Isolated Storage, i will not be able to achieve what i am looking for - Get a reference to the files chosen using photoChooserTask. I may be wrong.
Even though my initial idea was to add the Uri of Gallery file to my ImageCollection and store the index in database per item, Any suggestion or improvisation is appreciated.
Note: Added CameraCaptureTask Tag as PhotoChooserTask Tag is not available and the basic logic is same.
EDIT Code used to store and retrieve from IsolatedStorage
I am binding the result of GetImageFromIsolatedStorage() method to me Image control source. This doesnt work.. What am i doing wrong...
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;
}