2013-04-25 191 views
0

这是文件选取器的代码
我需要复制用户将其打开到应用程序文件夹的图像。 任何人都可以帮我请如何将图片从文件夹复制到应用程序文件夹窗口存储应用程序

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); 

//设置图像源选择bitmap.`

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

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


      } 
     } 

    } 

感谢

+0

那究竟是不是工作? – 2013-04-25 16:10:05

回答

0

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

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

相关问题