如何从filepiker复制图像到应用程序文件夹Windows商店应用程序
本文关键字:应用程序 文件夹 Windows 图像 filepiker 复制 | 更新日期: 2023-09-27 18:11:03
这是文件选择器
的代码我需要复制的图像,用户打开它的应用程序文件夹。有谁能帮我吗?
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;
}
}
}
谢谢
使用StorageFile。CopyAsync,即file.CopyAsync。第一个参数是目标存储文件夹,例如Windows.Storage.ApplicationData.Current.LocalFolder如果你想复制到appdata;否则,您需要创建一个文件夹或从选择器中单独获取一个文件夹。
例如,您可以让用户使用文件选择器(为文件夹配置)选择一个默认文件夹。只要确保将该StorageFolder保存在[Windows.Storage.AccessCache][2]
中,以保留编程访问以供将来使用。