This is example of what I'm trying to display with the label: It's not accurate yet, it should be line under line each start at the same location.
and the data should be in red the text should be in green.
First the red Download Progress: should not be displayed at all.
then the rest are line under line but it's all in red. the text Size: should be in green the 1.211 MB in red the text Downloaded: in green and 0.727 MB in red Images Left: green 4 red Speed: green 1.79 MB/s red
I tried to create a module class for that:
using System;
using System.Text;
public static class DownloadProgressFormatter
{
public static string FormatDownloadProgress(double totalMBs, double downloadedMBs, int imagesLeft, double speed)
{
StringBuilder progressBuilder = new StringBuilder();
Console.ForegroundColor = ConsoleColor.Green;
progressBuilder.AppendLine("Download Progress:");
Console.ForegroundColor = ConsoleColor.Red;
progressBuilder.AppendLine($" Size: {totalMBs} MB");
progressBuilder.AppendLine($" Downloaded: {downloadedMBs} MB");
Console.ForegroundColor = ConsoleColor.Green;
progressBuilder.AppendLine($" Images Left: {imagesLeft}");
Console.ForegroundColor = ConsoleColor.Red;
progressBuilder.AppendLine($" Speed: {speed} MB/s");
Console.ResetColor();
return progressBuilder.ToString();
}
}
then using it in form1:
private async Task DownloadImages(List<string> links)
{
int totalLinks = links.Count;
int downloadedCount = 0;
for (int i = 0; i < links.Count; i++)
{
string link = links[i];
using (var client = new WebClient())
{
client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
Stopwatch sw = Stopwatch.StartNew();
byte[] data = await client.DownloadDataTaskAsync(link);
sw.Stop();
string outputFolder = Properties.Settings.Default.SatelliteFolder;
string extension = ".png";
string filename = $"Satellite_{(i + 1).ToString("000")}{extension}";
string fullPath = Path.Combine(outputFolder, filename);
// Write the bytes to a file
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{
await stream.WriteAsync(data, 0, data.Length);
}
downloadedCount++;
// Calculate download progress information
int imagesLeft = totalLinks - downloadedCount;
double totalMBs = Math.Round((double)totalLinks * data.Length / 1024.0 / 1024.0, 3);
double downloadedMBs = Math.Round((double)downloadedCount * data.Length / 1024.0 / 1024.0, 3);
double speed = Math.Round((double)data.Length / 1024.0 / 1024.0 / sw.Elapsed.TotalSeconds, 3);
string progress = DownloadProgressFormatter.FormatDownloadProgress(totalMBs, downloadedMBs, imagesLeft, speed);
lblDownloadProgress.Text = progress;
lblDownloadProgress.Font = new Font(lblDownloadProgress.Font, FontStyle.Bold);
// Update progress bar
progressBar1.Value = (int)((double)downloadedCount / totalLinks * 100);
// Load the image from memory
Image image;
using (MemoryStream ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
}
// Add the image to the downloadImages list
downloadedImages.Add(image);
// Update the picture box with the latest image
pictureBox1.Image = image;
pictureBox1.Refresh();
await Task.Delay(100);
}
}
// Start the timer to display the downloaded images
timer1.Enabled = true;
}
the part:
// Calculate download progress information
int imagesLeft = totalLinks - downloadedCount;
double totalMBs = Math.Round((double)totalLinks * data.Length / 1024.0 / 1024.0, 3);
double downloadedMBs = Math.Round((double)downloadedCount * data.Length / 1024.0 / 1024.0, 3);
double speed = Math.Round((double)data.Length / 1024.0 / 1024.0 / sw.Elapsed.TotalSeconds, 3);
string progress = DownloadProgressFormatter.FormatDownloadProgress(totalMBs, downloadedMBs, imagesLeft, speed);
lblDownloadProgress.Text = progress;
lblDownloadProgress.Font = new Font(lblDownloadProgress.Font, FontStyle.Bold);
but it's all in red. the text for each property should be in green.
the label that displays the information is lblDownloadProgress