private async Task DownloadAsync()
{
using (var client = new WebClient())
{
client.DownloadFileCompleted = (s, e) =>
{
if (e.Error == null)
{
urlsCounter--;
var t = urls;
if (urlsCounter == 0)
{
CheckIfImagesExist();
btnRadarPath.Enabled = true;
btnSatellitePath.Enabled = true;
radCounter = 0;
satCounter = 0;
lblStatus.Text = "Completed.";
dates = rad.dates;
var images = System.IO.Directory.GetFiles(radarFolderImagesDownload,
"*.gif", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
Array.Sort(images, new MyComparer(false));
if (images.Length > 0)
{
for (int i = 0; i < images.Length; i )
{
drawOnImage.DrawText(dates[i].ToString("ddd, dd MMM yyy HH':'mm"), images[i]);
}
}
GetImagesFiles();
}
}
else
{
string error = e.Error.ToString();
}
};
client.DownloadProgressChanged = (s, e) => tracker.SetProgress(e.BytesReceived, e.TotalBytesToReceive);
client.DownloadProgressChanged = (s, e) => lblAmount.Text = tracker.SizeSuffix(e.BytesReceived) "/" tracker.SizeSuffix(e.TotalBytesToReceive);
client.DownloadProgressChanged = (s, e) => lblSpeed.Text = tracker.GetBytesPerSecondString();
client.DownloadProgressChanged = (s, e) => myLong = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
client.DownloadProgressChanged = (s, e) =>
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = e.ProgressPercentage "%";
};
for (int i = 0; i < urls.Count; i )
{
tracker.NewFile();
if (urls[i].Contains("Radar"))
{
await client.DownloadFileTaskAsync(new Uri(urls[i]), radarFolderImagesDownload "\\image" radCounter ".gif");
radCounter ;
}
else
{
using (MemoryStream ms = new MemoryStream(client.DownloadData(new Uri(urls[i]))))
{
Image img = Image.FromStream(ms, true);
img.Save(satelliteFolderImagesDownload "\\image" satCounter ".gif", System.Drawing.Imaging.ImageFormat.Gif);
}
satCounter ;
}
}
}
}
It's downloading fine everything but when it's downloading the satellite images part :
using (MemoryStream ms = new MemoryStream(client.DownloadData(new Uri(urls[i]))))
{
Image img = Image.FromStream(ms, true);
img.Save(satelliteFolderImagesDownload "\\image" satCounter ".gif", System.Drawing.Imaging.ImageFormat.Gif);
}
satCounter ;
It's not reporting progress as before.
Before I used to download this images the same as the Radar part :
await Client.DownloadFileTaskAsync(new Uri(urls[i]), fNameSat);
but because I want to save the satellite images as gif when they download I'm using now memorystream and image.save and this avoid it from being reporting progress to progressBar and all the other client.DownloadProgressChanged event/s
How can I make it use memorystream and save them as gif and keep reporting progress ?
CodePudding user response:
Try this:
List<string> Urls = new List<string>();
Urls.Add("a url");
Urls.Add("another url");
for(int i = 0; i < Urls.Count; i )
{
System.Net.WebClient WC = new System.Net.WebClient();
WC.DownloadFileAsync(new Uri(Urls[i]), @"The location you want to save the file...");
WC.DownloadFileCompleted = (s, e) =>
{
progressBar1.Value = progressBar1.Maximum / Urls.Count;
};
}
This will download all of the urls in the list.