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++;
                    }
                }
            }
        }它下载一切都很好,但当它下载卫星图像部分时:
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++;它没有像以前那样报告进度。
在我下载这张图片之前,它和雷达部分一样:
await Client.DownloadFileTaskAsync(new Uri(urls[i]), fNameSat);但是,因为我想在下载卫星图像时将其保存为gif格式,所以我使用了now memorystream和image.save,这样可以避免向progressBar和所有其他client.DownloadProgressChanged事件报告进度
我如何让它使用memorystream并将它们保存为gif并不断报告进度?
发布于 2021-11-03 20:57:28
试试这个:
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;
   };
}这将下载列表中的所有urls。
https://stackoverflow.com/questions/69829886
复制相似问题