[英]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.