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

本文关键字:字符串 变量 保存 照片 位置 | 更新日期: 2023-09-27 18:10:58

在我的应用程序中,用户使用PhotoChooserTask从Camera Roll中选择一张照片。我想将照片的位置保存到LogEntry类的字符串变量中,这样我就可以在以后需要时重新加载照片。

Log是LogEntry类项的ObservableCollection .

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

如果不将照片保存到LogEntry中的字节数组中,并且不将照片保存到IsolatedStorage中,我该如何做到这一点?

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

首先,我将所选图片的名称和相册的名称保存在每个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;
        }