question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked Paul-5034 commented

How can I use webclient download files save them as gif type images using memortstream but also to report progress?

 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 ?

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered Paul-5034 commented

I believe just using the *TaskAsync method should be enough to allow progress events to fire, so replacing:

client.DownloadData(new Uri(urls[i])))


With

await client.DownloadDataTaskAsync(new Uri(urls[i])))


When you used this method previously were DownloadProgressChanged events not firing at all, or were they firing but the progress staying at zero?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

previously both the DownloadProgressChanged and the progressBar were working fine in the Radar and Satellite.

now only the Radar is reporting the progress.

I will try now your solution and will report(progress) :)

0 Votes 0 ·

I tried your answer it's reporting to the progresshcanged events and to the progressbar but it's never getting to the completed event.


For testing I tried to download like before with this line

 await client.DownloadFileTaskAsync(new Uri(urls[i]), satelliteFolderImagesDownload + "\\image" + satCounter + ".gif");

And it's reporting progress and also getting to the completed event.

This is how I tried with your answer : it's reporting the progress but not getting to the completed event.

 using (MemoryStream ms = new MemoryStream(await client.DownloadDataTaskAsync(new Uri(urls[i]))))
                         {
                             Image img = Image.FromStream(ms, true);
                             img.Save(satelliteFolderImagesDownload + "\\image" + satCounter + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
                         }
0 Votes 0 ·

Ah this is possibly because you're using attaching a DownloadFileCompleted listener which is used by your Radar image download, but you'll want to attach DownloadDataCompleted as well to handle the DownloadDataTaskAsync completion too.

0 Votes 0 ·