Saving the photo to a class

谁说胖子不能爱 提交于 2019-11-27 21:34:57

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!

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);
            }
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!