簡體   English   中英

如何將圖像從Filepiker復制到應用程序文件夾Windows存儲應用程序

[英]how to copy image from filepiker to app folder windows store apps

這是文件選擇器的代碼
我需要將用戶打開的圖像復制到應用程序文件夾中。 任何人都可以幫助我

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。 第一個參數是目標StorageFolder,例如Windows.Storage.ApplicationData.Current.LocalFolder(如果要復制到appdata); 否則,您需要創建一個文件夾或從選擇器中單獨獲取一個。

例如,您可以讓用戶使用文件選擇器(為文件夾配置)選擇默認文件夾。 只需確保將StorageFolder保存在[Windows.Storage.AccessCache][2]以保留編程訪問權限以備將來使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM