将照片保存到一个类中

9

我想将从cameraCaptureTask获得的PhotoResult保存到我的类中,该类用作集合,然后保存到独立存储中:

void cameraCaptureTask_Completed(object sender, PhotoResult e)

这是ObservableCollection的一部分。我想将照片保存到此集合中。

[DataMember]
public Image VehicleImage
{
   get
   {
      return _vehicleImage;
   }
   set
   {
      if (value != _vehicleImage)
      {
         _vehicleImage = value;
         NotifyPropertyChanged("VehicleImage");
      }
   }
}

我将使用以下示例进行翻译:http://www.blog.ingenuitynow.net。在这个示例中,它能够正常工作,但是它设置了一个独立的独立存储,并且我想要加入到我的现有集合中。
我认为我不能使用Image类型。有什么最好的方法来实现我所希望的?
只是回答下面的评论。这就是.Save正在执行的操作:
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);
    }
}

我不太明白你的目的。你是通过序列化集合对象将集合保存到隔离存储中吗?你所说的集合是什么? - mostruash
@Jeff V,你有机会试试我的解决方案吗?我很肯定它会对你有用的,如果你需要进一步的帮助,请告诉我。 - MyKuLLSKI
3个回答

17
我想我终于找到了你的问题所在。在你的ObservableCollection中,我个人不会将图像保存在其中。相反,我会保留一个BitmapSource以使用更少的资源,但是你可能有自己的理由。
我的处理流程如下:
  1. 将Image.Source(BitmapSource)转换为byte []
  2. 将byte []保存到存储器中
  3. 从存储器中加载byte []
  4. 将byte []转换为Image.Source(BitmapSource)
将通用类型保存到独立存储中(在我的实用程序类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);
        }
    }
}

将通用文件加载到隔离存储中(在我的实用类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);
        }
    }
}

将BitmapSource转换为byte[](在我的实用程序类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();
    }
}

将byte[]转换为BitmapSource(在我的实用类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;
}

示例

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

请记住这是一个jpg保存。如果您想保存png,则需要使用CodePlex库或创建自己的PNG编码器。

希望这可以帮助您!


我正在做与这个解决方案非常相似的事情。 - earthling
我肯定测试过了,但是还没有获得赏金。感谢@earthling的评论。 - MyKuLLSKI

2

实际上在那篇博客中,他知道最近存储的ImageFileName,因此能够从隔离存储中检索相同的图像。根据您的评论,我不认为这个例子对您有帮助。

但是,如果您想将图片与对象一起存储,则必须将整个对象连同拍摄的图片序列化。

通过将您获得的流转换为byte[]数组来实现序列化图片,您可以再次将其从byte[]数组转换为BitmapImage。)

在此链接中解释了图像转换和序列化

使用此示例,您可以将整个对象序列化。


非常感谢您提供的这些信息。我今晚回家后会进行核实。 - webdad3
我今晚或明天会开始这个项目。我会告诉你的。 - webdad3

1
在这个例子中,我假设你已经有了一个ObservableCollection,你想要存储所有的图像,假设它的名称是VehicleImages。
所以在cameraCaptureTask_Completed事件中,你将从IsolatedStorage加载所有数据到VehicleImages中,现在你将新的VehicleImage添加到VehicleImages中,并将其保存到IsolatedStorage中。
保存和加载的代码:
            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);
            }
        }
    }

实际上,你已经接近成功了。我有一个名为vehicles的可观察集合,我真正想做的是将这个图片添加到其中。因此,我想知道在vehicles集合中,我需要为相机图片使用哪种类型,并且在拍摄完照片后如何保存它。 - webdad3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接