2013-04-25 134 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]中以保留程序訪問權限以供將來使用。

相關問題