我如何使用 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++;
}
}
}
}
它下载一切正常,但在下载卫星图像部分时:
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,所以我现在正在使用内存流和 image.save,这样可以避免它向 progressBar 和所有其他 client.DownloadProgressChanged 报告进度event/s
如何让它使用 memorystream 并将它们保存为 gif 并继续报告进度?
试试这个:
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;
};
}
这将下载列表中的所有 url。
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,所以我现在正在使用内存流和 image.save,这样可以避免它向 progressBar 和所有其他 client.DownloadProgressChanged 报告进度event/s
如何让它使用 memorystream 并将它们保存为 gif 并继续报告进度?
试试这个:
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;
};
}
这将下载列表中的所有 url。