如何存储位图图像本地或漫游?

时间:2012-09-27 16:46:26

标签: c# windows-8 windows-runtime winrt-xaml

我有一个定义Person的类。在课堂上,我的定义如下:

    [XmlIgnore]
    public BitmapImage PhotoSource
        {
        get { return _PhotoSource; }
        set
            {
            _PhotoSource = value;
            NotifyPropertyChanged( "PhotoSource" );
            }
        }

我添加了:

      [XmlIgnore]

VS。使用

      [DataMember]

因为序列化不适用于BitmapImage。

但是,我仍然需要保存BitmapImage Local和/或Roaming。

如何实现?

谢谢,  EitanB

1 个答案:

答案 0 :(得分:2)

从BitmapImage创建一个文件,然后使用StorageFile类将其写入文件系统。

    Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(bmImage.UriSource);
    await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);

将文件加载回BitmapImage:

BitmapImage bmImage;
bmImage = new BitmapImage();

bmImage.UriSource = new Uri(new Uri(
     Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" +
     Windows.Storage.ApplicationData.Current.LocalFolder.Name), 
     "favicon.scale-100.ico");