简体   繁体   中英

how to copy an image from the filepicker into anotehr folder via an UWP app?

I need to copy an image from my windows universal app, I was able to pick an image from my gallery but i don't how to copy it in another folder.

Although I was able to display the image in my UWP interface, so I think that I succeed to get it as a stream.

Any help would be appreciated, I'm lost here ... here is the code I used:

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

在此处输入图片说明 `

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM