windows-phone-7.1 – 将照片保存到课堂

我想将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,在该示例中它可以正常工作,但它正在设置一个单独的隔离存储,我只想加入我现有的集合.

我想我不能使用图像类型.什么是完成我希望做的最好的方法

只是为了回答下面的评论.这就是.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);
    }
}
我想我终于弄清楚了你的问题.在您的ObservableCollection中,我个人不会在那里保留图像.相反,我会保持BitmapSource使用较少的资源,但是您可能有理由为什么你这样做.

我的过程

>将Image.source(BitmapSource)转换为一个字节[]
>将字节[]保存到存储
>从存储器加载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,100);

        return stream.ToArray();
    }
}

将byte []转换为BitmapSource(在我的实用程序类中:Image_Utility.cs)

public static BitmapSource ByteArrayToImage(byte[] bytes)
{
    BitmapImage bitmapImage = null;
    using (MemoryStream stream = new MemoryStream(bytes,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.

我希望这有帮助!

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...