Now it's downloading all the files at once I mean the progressbar is moving from start to end once when all the files downloading instead how can I make as option that the progress bar will start from 0 to 100 per file download ?
And how to add a percentages text for example %34 I added a label already to the middle of the progressBar1.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override async void OnLoad(EventArgs e) {
var urls = new[] {
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
"https://upload.wikimedia.org/wikipedia/commons/d/d4/ESC_large_ISS008_ISS008-E-5604_Trinity_test_site_marked.jpg",
};
var filenames = new List<string>();
using var wc = new WebClient();
var progressPerUrl = 100 / (float)urls.Length;
for (var i = 0; i < urls.Length; i++) {
var url = urls[i];
wc.Headers.Add("User-Agent", "User-Agent: CoolTool/0.0 (https://example.org/cool-tool/; cool-tool@example.org) generic-library/0.0");
var filename = $"out-{i}.jpg";
filenames.Add(filename);
wc.DownloadProgressChanged += (e, sender) => {
var urlProgressStart = progressPerUrl * i;
progressBar1.Value = (int)(urlProgressStart + progressPerUrl * sender.ProgressPercentage / 100f);
};
await wc.DownloadFileTaskAsync(url, filename);
}
var saveTasks = new List<Task>();
foreach (var filename in filenames) {
saveTasks.Add(Task.Run(() => {
var newFilename = Path.ChangeExtension(filename, ".gif");
Image.FromFile(filename).Save(newFilename, ImageFormat.Gif);
}));
}
await Task.WhenAll(saveTasks);
}
}