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

Chocolade 536 Reputation points
2021-11-03T20:19:43.47+00:00
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 ?

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2021-11-03T21:12:11.68+00:00

    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?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.