将照片保存到班级 [英] Saving the photo to a class

查看:32
本文介绍了将照片保存到班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

void cameraCaptureTask_Completed(object sender, PhotoResult e)

这是 ObservableCollection 的一部分.我想将照片保存到此收藏集中.

[数据成员]公共图片 VehicleImage{得到{返回_vehicleImage;}放{如果(值!= _vehicleImage){_vehicleImage = 值;NotifyPropertyChanged("VehicleImage");}}}

我使用的示例来自:http://www.blog.ingenuitynow.net 并且在示例中它工作正常,但它正在设置一个单独的独立存储,我只想加入我现有的集合.>

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

只是为了回答下面的评论.这就是 .Save 正在做的事情:

public static void Save(string name, T objectToSave){使用 (IsolatedStorageFile storageFile = IndependentStorageFile.GetUserStoreForApplication())使用 (IsolatedStorageFileStream storageFileStream = new IndependentStorageFileStream(name, System.IO.FileMode.Create, storageFile)){DataContractSerializer 序列化器 = new DataContractSerializer(typeof(T));serializer.WriteObject(storageFileStream, objectToSave);}}

解决方案

我想我终于解决了您的问题.在您的 ObservableCollection 中,我个人不会在其中保留图像.相反,我会保留一个 BitmapSource 以使用更少的资源,但是您可能有理由这样做.

我的流程

  1. 将 Image.Source(BitmapSource) 转换为字节 []
  2. 将字节[]保存到存储中
  3. 从存储中加载字节[]
  4. 将 byte[] 转换为 Image.Source(BitmapSource)

将通用保存到独立存储(在我的实用程序类中:IsolatedStorage_Utility.cs)

public static void Save(string fileName, T item){使用(IsolatedStorageFile 存储=IsolatedStorageFile.GetUserStoreForApplication()){使用 (IsolatedStorageFileStream fileStream = new IndependentStorageFileStream(fileName, FileMode.Create, storage)){DataContractSerializer 序列化器 = new DataContractSerializer(typeof(T));serializer.WriteObject(fileStream, item);}}}

将通用加载到独立存储(在我的实用程序类中:IsolatedStorage_Utility.cs)

public static T Load(string fileName){使用(IsolatedStorageFile 存储=IsolatedStorageFile.GetUserStoreForApplication()){使用 (IsolatedStorageFileStream fileStream = new IndependentStorageFileStream(fileName, FileMode.Open, storage)){DataContractSerializer 序列化器 = new DataContractSerializer(typeof(T));返回 (T)serializer.ReadObject(fileStream);}}}

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

public static byte[] ImageToByteArray(BitmapSource bitmapSource){使用 (MemoryStream 流 = new MemoryStream()){WriteableBitmap writableBitmap = new WriteableBitmap(bitmapSource);Extensions.SaveJpeg(writableBitmap, stream, bitmapSource.PixelWidth, bitmapSource.PixelHeight, 0, 100);返回流.ToArray();}}

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

public static BitmapSource ByteArrayToImage(byte[] bytes){BitmapImage bitmapImage = null;使用 (MemoryStream 流 = 新的 MemoryStream(bytes, 0, bytes.Length)){bitmapImage = new BitmapImage();bitmapImage.SetSource(stream);}返回位图图像;}

示例

private void TestImageConversion(object sender, RoutedEventArgs e){byte[] image1AsByteArray = Image_Utility.ImageToByteArray((BitmapSource)Image1.Source);IndependentStorage_Utility.Save("Image1.jpg", image1AsByteArray);BitmapSource image1AsBitmapImage = Image_Utility.ByteArrayToImage(IsolatedStorage_Utility.Load("Image1.jpg"));Image2.Source = image1AsBitmapImage;}

请记住,这是一个 jpg 格式的文件.如果要保存 png,则需要使用 CodePlex 库或创建自己的 PNGEncoder.

我希望这会有所帮助!

I would like to save the PhotoResult from the cameraCaptureTask into my class which I'm using as a collection and then saving into Isolated Storage:

void cameraCaptureTask_Completed(object sender, PhotoResult e)

This is part of an ObservableCollection. I want to save the photo into this collection.

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

I'm using the example from: http://www.blog.ingenuitynow.net and in the example it works fine, but it is setting up an individual Isolated Storage and I would just like to join to my existing collection.

I'm thinking that I can't use the Image type. What would be the best way to accomplish what I'm hoping to do?

Just to answer the comment below. This is what the .Save is doing:

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

解决方案

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!

这篇关于将照片保存到班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆