我想将PhotoResult从cameraCaptureTask保存到我的类中,并将其用作集合,然后保存到隔离存储中:
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,在这个示例中,它运行得很好,但是它正在设置一个单独的独立存储,我只想加入到我现有的集合中。
我在想我不能使用图像类型。要完成我希望做的事情,最好的方法是什么?
只是为了回答下面的评论。这就是.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);
}
}发布于 2012-01-24 04:51:29
我想我终于解决了你的问题。在您的ObservableCollection中,我个人不会在其中保留一个图像。相反,我会保留一个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转换为字节在我的实用工具类: 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库或创建自己的PNGEncoder。
我希望这能帮到你!
发布于 2012-01-23 18:38:20
实际上,在那个博客中,他知道最近存储的ImageFileName,所以他能够从隔离存储中提取相同的图像。根据你的评论,我不认为这个例子对你有帮助。
但是,如果您想将图片与对象一起存储,则意味着您必须将整个对象与所拍摄的图片一起序列化。
序列化图片的方法是将流转换为 byte[] 数组,并且可以再次将byte[]数组转换为BitmapImage )。
在这个链接中,图像转换和序列化在这里被删除。
使用此示例,您可以使用整个对象进行序列化。
发布于 2012-01-21 12:52:06
在本例中,除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);
}
}
}https://stackoverflow.com/questions/8950525
复制相似问题