使用文件保存选择器C#Win 8 Metro App将运行时生成的图像保存到图片库中?

时间:2013-08-05 17:00:42

标签: c# image microsoft-metro

我不知道如何将图像复制到图书馆 请帮助我

private async void Button_Click_2(object sender, RoutedEventArgs e)
    {

        if (img != null)
        {
           try
            {
                Windows.Storage.Pickers.FileSavePicker SavePicker = new Windows.Storage.Pickers.FileSavePicker();
                SavePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
                SavePicker.DefaultFileExtension = ".png";
                SavePicker.FileTypeChoices.Add("PNG", new string[] { ".png" });
                SavePicker.FileTypeChoices.Add("JPG", new string[] { ".jpg" });
                StorageFile filesave = await SavePicker.PickSaveFileAsync();
            }

            catch (Exception)
            {
                var MsgDlg = new MessageDialog("Oops something went wrong.", "Error while saving");
                MsgDlg.ShowAsync();
            }
        }
        else
        {
            var MsgDlg = new MessageDialog("Ooops Something went wrong.", "Error while saving");
            await MsgDlg.ShowAsync();
        }

1 个答案:

答案 0 :(得分:0)

var response = await HttpWebRequest.Create(url).GetResponseAsync();
                List<Byte> allBytes = new List<byte>();
                using (Stream imageStream = response.GetResponseStream())
                {
                    byte[] buffer = new byte[4000];
                    int bytesRead = 0;
                    while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0)
                    {
                        allBytes.AddRange(buffer.Take(bytesRead));
                    }
                }

                StorageFolder storageFolder = awai KnownFolders.PicturesLibrary.CreateFolderAsync("QR Codes Generator",CreationCollisionOption.OpenIfExists );

                //var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                //            System.IO.Path.GetRandomFileName()+".png", CreationCollisionOption.FailIfExists);

                // var folder = await StorageFolder.GetFolderFromPathAsync("");

                var file = await storageFolder.CreateFileAsync(
                System.IO.Path.GetRandomFileName() + ".png", CreationCollisionOption.FailIfExists);
                await FileIO.WriteBytesAsync(file, allBytes.ToArray());
            MessageDialog a = new MessageDialog("QR Code saved successfully!","QR Codes Generator");
                a.ShowAsync();