//gu function public static async Task <BitmapImage> BrowseImage() { FileOpenPicker picker = new FileOpenPicker(); picker.SuggestedStartLocation = PickerLocationId.Desktop; picker.ViewMode = PickerViewMode.Thumbnail; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".bmp"); GameCover = await picker.PickSingleFileAsync(); BitmapImage img = new BitmapImage(); if (GameCover != null) { using (var imgStream = await GameCover.OpenReadAsync()) { img.SetSource(imgStream); } } return(img); }
//download picture & save public async static Task <bool> DownloadPic(string imgLink, string imgName) { StorageFile GameCover; StorageFolder localFolder = ApplicationData.Current.LocalFolder; try { //imgName = imgName + Path.GetExtension(imgLink); using (var httpClient = new HttpClient()) { var data = await httpClient.GetByteArrayAsync(new Uri(imgLink)); GameCover = await localFolder.CreateFileAsync(imgName, CreationCollisionOption.ReplaceExisting); var targetStream = await GameCover.OpenAsync(FileAccessMode.ReadWrite); await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length); await targetStream.FlushAsync(); targetStream.Dispose(); } } catch (Exception) { } return(true); }
private async void btnEditGame_Click(object sender, RoutedEventArgs e) { btnEditGame.IsEnabled = false; ringEditGame.IsActive = true; if (imgGameCover.Source != null) { int i = 0; foreach (cGame game in App.gvm.GamesCollection) { //await new MessageDialog("chk " + game.Name).ShowAsync(); if (game.Name == Game.Name) { //await new MessageDialog("chk").ShowAsync(); App.gvm.GamesCollection[i].Name = txtName.Text; App.gvm.GamesCollection[i].Path = txtPath.Text; App.gvm.GamesCollection[i].Description = txtDesc.Text; App.gvm.GamesCollection[i].ReleaseDate = txtDate.Text; //await new MessageDialog("chk " + App.gvm.GamesCollection[i].Path).ShowAsync(); try { string imgName; //for downloaded image if (bImgDown != null && GameCover == null) { imgName = Path.GetFileName(imgLink);// txtName.Text + Path.GetExtension(imgLink); #region Dowload Image //download image code start var httpClient = new HttpClient(); //httpClient.MaxResponseContentBufferSize = 256; var data = await httpClient.GetByteArrayAsync(new Uri(imgLink)); GameCover = await localFolder.CreateFileAsync(imgName, CreationCollisionOption.ReplaceExisting); var targetStream = await GameCover.OpenAsync(FileAccessMode.ReadWrite); await targetStream.AsStreamForWrite().WriteAsync(data, 0, data.Length); await targetStream.FlushAsync(); targetStream.Dispose(); //download image code end #endregion } else { imgName = Path.GetFileName(GameCover.Name);// txtName.Text + Path.GetExtension(GameCover.Name); try { StorageFile file = await localFolder.CreateFileAsync(imgName, CreationCollisionOption.ReplaceExisting); await GameCover.CopyAndReplaceAsync(file); } catch (Exception) { } } App.gvm.GamesCollection[i].Image = "ms-appdata:///local/" + imgName; } catch (Exception) { } break; } i++; } cUserData.AddNewGame(); await cUserData.GetGames(); GameCover = null; if (this.Frame.CanGoBack) { this.Frame.GoBack(); } cGuFuncs.CreateToast("GameR Desk", txtName.Text + " Details Updated in Library."); } else { await new MessageDialog("Please Select Game Cover").ShowAsync(); } btnEditGame.IsEnabled = true; ringEditGame.IsActive = false; }