如何通过UWP应用程序将图像从filepicker复制到anotehr文件夹?

时间:2016-03-13 04:50:26

标签: c# stream win-universal-app bitmapimage

我需要从我的Windows通用应用程序中复制图像,我能够从我的图库中选择一个图像,但我不知道如何将其复制到另一个文件夹中。

虽然我能够在我的UWP界面中显示图像,但我认为我成功将其作为流媒体。

任何帮助都将不胜感激,我迷失在这里......这是我使用的代码:

public MainPage()
{
        this.InitializeComponent();
       // Scenario4WriteableBitmap = new WriteableBitmap((int)Scenario4ImageContainer.Width, (int)Scenario4ImageContainer.Height);
        Scenario2DecodePixelHeight.Text = "100";
        Scenario2DecodePixelWidth.Text = "100";
}

private async void buttonUpload_Click(object sender, RoutedEventArgs e)
{
        int decodePixelHeight=150;
        int decodePixelWidth=150;

        // Try to parse an integer from the given text. If invalid, default to 100px
        if (!int.TryParse(Scenario2DecodePixelHeight.Text, out decodePixelHeight))
        {
            Scenario2DecodePixelHeight.Text = "100";
            decodePixelHeight = 100;
        }

        // Try to parse an integer from the given text. If invalid, default to 100px
        if (!int.TryParse(Scenario2DecodePixelWidth.Text, out decodePixelWidth))
        {
            Scenario2DecodePixelWidth.Text = "100";
            decodePixelWidth = 100;
        }

        FileOpenPicker open = new FileOpenPicker();
        open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        open.ViewMode = PickerViewMode.Thumbnail;

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

        // Open a stream for the selected file
        StorageFile file = await open.PickSingleFileAsync();

        // Ensure a file was selected
        if (file != null)
        {
            // Ensure the stream is disposed once the image is loaded
            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                // Set the image source to the selected bitmap
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.DecodePixelHeight = decodePixelHeight;
                bitmapImage.DecodePixelWidth = decodePixelWidth;

                await bitmapImage.SetSourceAsync(fileStream);
         Scenario2Image.Source = bitmapImage;
            }
        }
}

private async void submit_Click(object sender, RoutedEventArgs e)
{
        String url = "http://localhost/mydatabase/add.php";

        var values = new List<KeyValuePair<String, String>>
        {
            new KeyValuePair<string, string>("UserName",UserName.Text),
            new KeyValuePair<string, string>("UserImage",UserImage.Text),
        };

        HttpClient client = new HttpClient();

        HttpResponseMessage response = new HttpResponseMessage();

        try
        {
            response = await client.PostAsync(url, new FormUrlEncodedContent(values));

            /*client.DefaultRequestHeaders.Add("content_type", "binary/octet_stream");
            responseImage = client.PostAsync("", FileChooser.FileName);*/

            if (response.IsSuccessStatusCode)
            {
                Debug.WriteLine(response.StatusCode.ToString());
                var dialog = new MessageDialog("added succesfully ");
                await dialog.ShowAsync();
            }
            else
            {
                // problems handling here
                string msg = response.IsSuccessStatusCode.ToString();

                throw new Exception(msg);
            }
        }
        catch (Exception exc)
        {
            // .. and understanding the error here
            Debug.WriteLine(exc.ToString());
        }
}

enter image description here`

1 个答案:

答案 0 :(得分:0)

您可以使用CopyAsync StorageFile将从FileOpenPicker获取的文件复制到特定文件夹。

相关问题