简体   繁体   English

如何使用 webclient 下载文件,使用 memortstream 将它们保存为 gif 类型图像,同时还报告进度?

[英]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但是因为我想在下载卫星图像时将它们保存为 gif,所以我现在正在使用 memorystream 和 image.save,这可以避免它向 progressBar 和所有其他 client.DownloadProgressChanged event/s 报告进度

How can I make it use memorystream and save them as gif and keep reporting progress ?我怎样才能让它使用内存流并将它们保存为 gif 并继续报告进度?

Try this:尝试这个:

System.Net.WebClient WC = new System.Net.WebClient();

List<string> Urls = new List<string>();

Urls.Add("a url");
Urls.Add("another url");

for(int i = 0; i < Urls.Count; i++)
{
   WC.DownloadFileAsync(new Uri(Urls[i]), @"The location you want to save the file too");
}
WC.DownloadFileCompleted += (s, e) => 
{
   progressBar1.Value += progressBar1.Maximum / Urls.Count;
};

This will download all of the urls in the list.这将下载列表中的所有 url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用两个progressBar控件显示每个文件的下载进度以及所有文件下载的整体进度? - How can i use two progressBar controls to display each file download progress and also overall progress of all the files download? 如何使用WebClient异步下载许多图像? - How can i download many images async using WebClient? Webclient 无法下载 .gif 文件吗? - Is Webclient unable to download .gif files? 使用WebClient下载显示进度 - Show Progress Using WebClient Download 如何在C#中使用WebClient下载多个文件? - How can I download multiple files with WebClient in C#? 我如何以适当的方式报告使用后台工作人员的文件图像转换进度? - How do i report in a proper way the progress of a files images convertion using a backgroundworker? 如何取消和进度报告WebClient.DownloadDataTaskAsync方法返回的任务? - How can I cancel and progress report a task that returned by WebClient.DownloadDataTaskAsync method? 如何使用webclient和线程下载多个文件? - How to download multiple files using webclient and threading? 使用Webclient下载多个文件 - Use Webclient to download several files How Can I save Ribbon Settings made in the Outlook Explorer Window and also display und use them in the Outlook MailItem Window? - How Can I save Ribbon Settings made in the Outlook Explorer Window and also display und use them in the Outlook MailItem Window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM