I would like to save the PhotoResult
from the cameraCaptureTask
into my class which I'm using as a collection and then saving into Isolated Storage:
void cameraCaptureTask_Completed(object sender, PhotoResult e)
This is part of an ObservableCollection. I want to save the photo into this collection.
[DataMember]
public Image VehicleImage
{
get
{
return _vehicleImage;
}
set
{
if (value != _vehicleImage)
{
_vehicleImage = value;
NotifyPropertyChanged("VehicleImage");
}
}
}
I'm using the example from: http://www.blog.ingenuitynow.net and in the example it works fine, but it is setting up an individual Isolated Storage and I would just like to join to my existing collection.
I'm thinking that I can't use the Image type. What would be the best way to accomplish what I'm hoping to do?
Just to answer the comment below. This is what the .Save
is doing:
public static void Save<T>(string name, T objectToSave)
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
using (IsolatedStorageFileStream storageFileStream = new IsolatedStorageFileStream(name, System.IO.FileMode.Create, storageFile))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(storageFileStream, objectToSave);
}
}
I think I finally figured out your issue. In your ObservableCollection I personally would not keep an image in there. Instead I would keep a BitmapSource to use less resources, however you may have reasoning why your doing that.
public static void Save<T>(string fileName, T item)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(fileStream, item);
}
}
}
public static T Load<T>(string fileName)
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(fileStream);
}
}
}
public static byte[] ImageToByteArray(BitmapSource bitmapSource)
{
using (MemoryStream stream = new MemoryStream())
{
WriteableBitmap writableBitmap = new WriteableBitmap(bitmapSource);
Extensions.SaveJpeg(writableBitmap, stream, bitmapSource.PixelWidth, bitmapSource.PixelHeight, 0, 100);
return stream.ToArray();
}
}
public static BitmapSource ByteArrayToImage(byte[] bytes)
{
BitmapImage bitmapImage = null;
using (MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length))
{
bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
}
return bitmapImage;
}
private void TestImageConversion(object sender, RoutedEventArgs e)
{
byte[] image1AsByteArray = Image_Utility.ImageToByteArray((BitmapSource)Image1.Source);
IsolatedStorage_Utility.Save<byte[]>("Image1.jpg", image1AsByteArray);
BitmapSource image1AsBitmapImage = Image_Utility.ByteArrayToImage(IsolatedStorage_Utility.Load<byte[]>("Image1.jpg"));
Image2.Source = image1AsBitmapImage;
}
Keep in mind this is a jpg saving. If you want to save a png thn you need to use a library of CodePlex or create your own PNGEncoder.
I hope this helps!
Actually in that blog, he knows the ImageFileName stored recently so he is able to retreive the same image from the Isolated storage. i dont think so that example helps you according to your comment.
But If you want store the Picture along with the object means you have to serialize whole object along with the picture taken.
serializing the picture is achieved by converting stream you got to byte[] array and you can convert from byte[] array to BitmapImage again.)
Image conversion and serialization is expalianed here in this link
use this sample and you can serialize with whole object.
In this example I'm excepting that you got ObservableCollection where you want to store all of the images lets say it's name is VehicleImages.
So at the cameraCaptureTask_Completed you load all of the data from IsolatedStorage to VehicleImages and now you add the new VehicleImage to VehicleImages and save it to IsolatedStorage.
Code for save and load:
public static void Save<T>(string name, T objectToSave)
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream storageFileStream = new IsolatedStorageFileStream(name, System.IO.FileMode.Create, storageFile))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(storageFileStream, objectToSave);
}
}
}
public ObservableCollection<T> Read<T>(string name)
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream storageFileStream = new IsolatedStorageFileStream(name, System.IO.FileMode.Open, storageFile))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return (ObservableCollection<T>)serializer.ReadObject(storageFileStream);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With