Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the photo to a class

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);
    }
}
like image 491
webdad3 Avatar asked Jan 21 '12 03:01

webdad3


3 Answers

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.

My Process

  1. Convert the Image.Source(BitmapSource) to a byte[]
  2. Save the byte[] to storage
  3. Load the byte[] from storage
  4. Convert the byte[] to and a Image.Source(BitmapSource)

Save Generic To Isolated Storage (In my utility class: IsolatedStorage_Utility.cs)

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);
        }
    }
}

Load Generic To Isolated Storage (In my utility class: IsolatedStorage_Utility.cs)

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);
        }
    }
}

Convert BitmapSource to byte[] (In my utility class: Image_Utility.cs)

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();
    }
}

Convert byte[] to BitmapSource (In my utility class: Image_Utility.cs)

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;
}

Example

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!

like image 69
MyKuLLSKI Avatar answered Sep 30 '22 22:09

MyKuLLSKI


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.

like image 28
Santhu Avatar answered Sep 30 '22 21:09

Santhu


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);
            }
        }
    }
like image 32
Teemu Tapanila Avatar answered Sep 30 '22 22:09

Teemu Tapanila