如何将照片位置保存为类的字符串变量?

时间:2013-04-24 23:40:08

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

在我的应用程序中,用户使用PhotoChooserTask从相机胶卷中选择照片。我想将照片的位置保存到LogEntry类的字符串变量中,以便我可以根据需要稍后重新加载照片。

Log是LogEntry类项的ObservableCollection。

public ObservableCollection<LogEntry> Log = new ObservableCollection<LogEntry>();

如果不将Photo保存到LogEntry中的Byte数组中而不将Photo保存到IsolatedStorage中,我怎么能这样做呢?

1 个答案:

答案 0 :(得分:0)

首先,我在每个LogEntry项目的属性中保存所选图片的名称和相册的名称。

从相册中加载图片:

        public BitmapImage GetPicture( string FileName )
        {
        // Work around for known bug in the media framework.  Hits the static constructors
        // so the user does not need to go to the picture hub first.
        MediaPlayer.Queue.ToString();

        MediaLibrary ml = null;
        PictureAlbum cameraRoll = null;
        PictureAlbum savedPictures = null;
        PictureAlbum samplePictures = null;
        PictureAlbum favoritePictures = null;

        int index = FileName.IndexOf( "\\" );
        string albumName = FileName.Remove( index, FileName.Length - index );
        string fileName = FileName.Remove( 0, index + 1 );

        foreach ( MediaSource source in MediaSource.GetAvailableMediaSources() )
            {
            if ( source.MediaSourceType == MediaSourceType.LocalDevice )
                {
                ml = new MediaLibrary( source );
                PictureAlbumCollection allAlbums = ml.RootPictureAlbum.Albums;

                foreach ( PictureAlbum album in allAlbums )
                    {
                    if ( album.Name == "Camera Roll" )
                        {
                        cameraRoll = album;
                        }
                    else if ( album.Name == "Saved Pictures" )
                        {
                        savedPictures = album;
                        }
                    else if ( album.Name == "Sample Pictures" )
                        {
                        samplePictures = album;
                        }
                    else if ( album.Name == "Favorite Pictures" )
                        {
                        favoritePictures = album;
                        }
                    }
                }
            }

        PictureAlbum Album;
        switch ( albumName )
            {
            case "Camera Roll":
                Album = cameraRoll;
                break;

            case "Saved Pictures":
                Album = savedPictures;
                break;

            case "Sample Pictures":
                Album = samplePictures;
                break;

            case "Favorite Pictures":
                Album = favoritePictures;
                break;

            default:
                Album = null;
                break;
            }

        if ( Album == null )
            {
            return new BitmapImage();
            }

        BitmapImage b = new BitmapImage();
        foreach ( Picture p in Album.Pictures.Take( Album.Pictures.Count ) )
            {
            if ( fileName == p.Name )
                {
                b.SetSource( p.GetThumbnail() );
                break;
                }
            }

        return b;
        }