如何使用ApplicationData保存和检索BitmapImage

时间:2014-06-22 00:23:56

标签: c# windows-phone-8 asynchronous async-await application-data

在引用http://tizianocacioppolini.blogspot.com/2014/01/windows-phone-8-folders-and-files.html#.U6YbkfhOXIU时,我正在尝试整理一个示例,我可以使用PhotoChooserTask收集照片,并使用AppicationData保存/检索此照片。最终目标是使用新的ApplicationData方法将图像保存在文件夹中,如果文件夹已存在,请删除并替换它。我不确定如何正确设置

MainPage.xaml.cs中

protected override void OnNavigatedTo(NavigationEventArgs e)
{
        base.OnNavigatedTo(e);

        //If an image exists in the folder, set the image to the page background using TombstoningHelper.cs Retrieve method

}

private void Browse_Click(object sender, RoutedEventArgs e)
    {
        photoChooserTask.Show();
    }

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            // get the file stream and file name
            Stream photoStream = e.ChosenPhoto;
            string fileName = Path.GetFileName(e.OriginalFileName);

            //Save the Image to storage using TombstoningHelper.cs
        }
    }

TombstoningHelper.cs

public async Task StorePhoto(Stream photoStream, string fileName)
    {
        StorageFolder folder;

        //Check if folder exists

        //If folder exists, delete it


        // persist data into isolated storage
        StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

        using (Stream current = await file.OpenStreamForWriteAsync())
        {
            await photoStream.CopyToAsync(current);
        }
    }

    public async Task<BitmapImage> RetrievePhoto(string fileName)
    {
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
        Stream imageStream = await file.OpenStreamForReadAsync();

        //Check if file exists

        // display the file as image
        BitmapImage bi = new BitmapImage();
        bi.SetSource(imageStream);

        return bi;
    }

0 个答案:

没有答案