private async void extractButton_Click(object sender, EventArgs e) { //Checks that ensures that a valid input was made if (textBox1.Text == string.Empty || textBox1.Text == " ") { MessageBox.Show("Textbox cannot be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Text = string.Empty; return; } if (!Uri.IsWellFormedUriString(textBox1.Text, UriKind.Absolute)) { MessageBox.Show("URL is not in correct format", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); textBox1.Text = string.Empty; return; } imageList.Clear(); listBox1.Items.Clear(); fileExtension = null; url = textBox1.Text; label1.Text = "Extracting data."; //Async calling methods CallingMethodsAsync caller = new CallingMethodsAsync(); try { imageList = await caller.DownloadAsync(textBox1.Text); } catch (Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } foreach (var imageLink in imageList) { listBox1.Items.Add(imageLink); } if (imageList.Count == 0) { MessageBox.Show("No images found", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } label1.Text = string.Format("Found {0} images.", imageList.Count); }
private async Task SaveAllImages(List <string> imageList) { //Have user set folderBrowserDialog1.SelectedPath property folderBrowserDialog1.SelectedPath = string.Empty; folderBrowserDialog1.ShowDialog(); string path = folderBrowserDialog1.SelectedPath; if (path == string.Empty) { return; } FormatListItems(imageList); CallingMethodsAsync caller = new CallingMethodsAsync(); ImageValidationMethods validator = new ImageValidationMethods(); for (int i = 0; i < imageList.Count; i++) { try { byteList.Add(await caller.DownloadImageAsync(imageList[i])); } catch (Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } label1.Text = string.Format("Downloaded {0} of {1} images.", byteList.Count, imageList.Count); } int counter = 0; for (int i = 0; i < byteList.Count; i++) { if (byteList[i].Length > 1) { if (validator.IsValidImageFormat(byteList[i])) { fileExtension = validator.ReturnImageExtension(byteList[i]); try { await File.WriteAllBytesAsync(Path.Combine(path, "image" + i + fileExtension), byteList[i]); } catch (Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { counter++; } } else { counter++; } } if (counter != 0) { MessageBox.Show(counter == 1 ? $"{counter} image could not be saved" : $"{counter} images could not be saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); } }