如何将图像从filepiker复制到应用程序文件夹windows存储应用程序

时间:2013-04-25 13:32:36

标签: c# windows-store-apps

这是文件选择器的代码
我需要将用户打开它的图像复制到app文件夹。 任何人都可以帮助我

private async void Button_Click(object sender, RoutedEventArgs e)
    {

        if (Windows.UI.ViewManagement.ApplicationView.Value != Windows.UI.ViewManagement.ApplicationViewState.Snapped ||
             Windows.UI.ViewManagement.ApplicationView.TryUnsnap() == true)
        {
            Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;

            // Filter to include a sample subset of file types.
            openPicker.FileTypeFilter.Clear();
            openPicker.FileTypeFilter.Add(".bmp");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".jpg");

//打开文件选择器。

        Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

            // file is null if user cancels the file picker.
            if (file != null)
            {
                // Open a stream for the selected file.
                Windows.Storage.Streams.IRandomAccessStream fileStream =
                    await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

//将图像源设置为选定的位图。

                Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
                    new Windows.UI.Xaml.Media.Imaging.BitmapImage();

                bitmapImage.SetSource(fileStream);
                img.Source = bitmapImage;
                this.DataContext = file;


            }
        }

    }

谢谢

1 个答案:

答案 0 :(得分:0)

使用StorageFile.CopyAsync,即file.CopyAsync。第一个参数是目标StorageFolder,例如Windows.Storage.ApplicationData.Current.LocalFolder如果要复制到appdata;否则你需要单独创建一个文件夹或从选择器中获取一个文件夹。

例如,您可以让用户选择带文件选择器的默认文件夹(为文件夹配置)。请务必在[Windows.Storage.AccessCache][2]中保存该StorageFolder,以保留程序化访问权限,以备将来使用。