Daniel Lip

Daniel Lip

  • NA
  • 64
  • 21.9k

How can I download multiple images and show them in a pictureBox ?

Dec 15 2020 1:09 AM
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.Net;  
  9. using System.Text;  
  10. using System.Threading;  
  11. using System.Threading.Tasks;  
  12. using System.Windows.Forms;  
  13. using HtmlAgilityPack;  
  14.   
  15. namespace SatImages  
  16. {  
  17.     public partial class Form1 : Form  
  18.     {  
  19.         private int counter = 0;  
  20.   
  21.         public Form1()  
  22.         {  
  23.               
  24.   
  25.             InitializeComponent();  
  26.   
  27.             backgroundWorker1.RunWorkerAsync();  
  28.         }  
  29.   
  30.         private void Form1_Load(object sender, EventArgs e)  
  31.         {  
  32.   
  33.         }  
  34.   
  35.         private void Download()  
  36.         {  
  37.             var wc = new WebClient();  
  38.             wc.BaseAddress = "https://en.sat24.com/en/tu/infraPolair";  
  39.             HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();  
  40.   
  41.             var temp = wc.DownloadData("/en");  
  42.             doc.Load(new MemoryStream(temp));  
  43.   
  44.             var secTokenScript = doc.DocumentNode.Descendants()  
  45.                 .Where(e =>  
  46.                        String.Compare(e.Name, "script"true) == 0 &&  
  47.                        String.Compare(e.ParentNode.Name, "div"true) == 0 &&  
  48.                        e.InnerText.Length > 0 &&  
  49.                        e.InnerText.Trim().StartsWith("var region")  
  50.                       ).FirstOrDefault().InnerText;  
  51.             var securityToken = secTokenScript;  
  52.             securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));  
  53.             securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('""").Replace("')""");  
  54.             var dates = securityToken.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);  
  55.             var scriptDates = dates.Select(x => new ScriptDate { DateString = x });  
  56.             foreach (var date in scriptDates)  
  57.             {  
  58.                 string img = "https://en.sat24.com/image?type=infraPolair®ion=tu×tamp=" + date.DateString;  
  59.   
  60.                 Image image = DownloadImageFromUrl(img);  
  61.                 image.Save(@"d:\satimages\" + counter + ".jpg");  
  62.                 counter++;  
  63.             }  
  64.         }  
  65.   
  66.         public class ScriptDate  
  67.         {  
  68.             public string DateString { getset; }  
  69.             public int Year  
  70.             {  
  71.                 get  
  72.                 {  
  73.                     return Convert.ToInt32(this.DateString.Substring(0, 4));  
  74.                 }  
  75.             }  
  76.             public int Month  
  77.             {  
  78.                 get  
  79.                 {  
  80.                     return Convert.ToInt32(this.DateString.Substring(4, 2));  
  81.                 }  
  82.             }  
  83.             public int Day  
  84.             {  
  85.                 get  
  86.                 {  
  87.                     return Convert.ToInt32(this.DateString.Substring(6, 2));  
  88.                 }  
  89.             }  
  90.             public int Hours  
  91.             {  
  92.                 get  
  93.                 {  
  94.                     return Convert.ToInt32(this.DateString.Substring(8, 2));  
  95.                 }  
  96.             }  
  97.             public int Minutes  
  98.             {  
  99.                 get  
  100.                 {  
  101.                     return Convert.ToInt32(this.DateString.Substring(10, 2));  
  102.                 }  
  103.             }  
  104.         }  
  105.   
  106.         public System.Drawing.Image DownloadImageFromUrl(string imageUrl)  
  107.         {  
  108.             System.Drawing.Image image = null;  
  109.   
  110.             try  
  111.             {  
  112.                 System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);  
  113.                 webRequest.AllowWriteStreamBuffering = true;  
  114.                 webRequest.Timeout = 30000;  
  115.   
  116.                 System.Net.WebResponse webResponse = webRequest.GetResponse();  
  117.   
  118.                 System.IO.Stream stream = webResponse.GetResponseStream();  
  119.   
  120.                 image = System.Drawing.Image.FromStream(stream);  
  121.   
  122.                 webResponse.Close();  
  123.             }  
  124.             catch (Exception ex)  
  125.             {  
  126.                 return null;  
  127.             }  
  128.   
  129.             return image;  
  130.         }  
  131.   
  132.         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
  133.         {  
  134.             Download();  
  135.         }  
  136.     }  
  137. }  
It's downloading the images but I have some problems :
 
 * It's downloading all the images at once fast and I want to add some small delay between each downloaded image.
 
 * I want to display the downloaded images one by one in pictureBox1 so it will give some animation of the images in the pictureBox, that's why I need the delay in the first problem I mentioned above. I think I need a delay for it but the main goal is to create some kind of animated gif a like inside the pictureBox.
 
 * How to use report progress and progressBar to display the percentages it's taking to download all the images ?
    I have already a progressBar in the designer and I set already the backgroundworker WorkerReportsProgress      and WorkerSupportsCancellation both to true.
 
 Overall the main goal is to download the images one by one to the hard disk save them as files like I'm doing now and also to display the images in pictureBox creating animated gif illusion in the pictureBox and using the progressBar.

Answers (1)