加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:45:58 所属栏目:Windows 来源:网络整理
导读:我想将CameraCaptureTask中的PhotoResult保存到我作为集合使用的类中,然后保存到隔离存储中: void cameraCaptureTask_Completed(object sender,PhotoResult e) 这是ObservableCollection的一部分.我想将照片保存到此集合中. [DataMember]public Image Vehic
我想将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.

我希望这有帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读