TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Daniel Lip
NA
64
22k
How can I download multiple images and show them in a pictureBox ?
Dec 15 2020 1:09 AM
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.IO;
using
System.Linq;
using
System.Net;
using
System.Text;
using
System.Threading;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
HtmlAgilityPack;
namespace
SatImages
{
public
partial
class
Form1 : Form
{
private
int
counter = 0;
public
Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
}
private
void
Download()
{
var wc =
new
WebClient();
wc.BaseAddress =
"https://en.sat24.com/en/tu/infraPolair"
;
HtmlAgilityPack.HtmlDocument doc =
new
HtmlAgilityPack.HtmlDocument();
var temp = wc.DownloadData(
"/en"
);
doc.Load(
new
MemoryStream(temp));
var secTokenScript = doc.DocumentNode.Descendants()
.Where(e =>
String.Compare(e.Name,
"script"
,
true
) == 0 &&
String.Compare(e.ParentNode.Name,
"div"
,
true
) == 0 &&
e.InnerText.Length > 0 &&
e.InnerText.Trim().StartsWith(
"var region"
)
).FirstOrDefault().InnerText;
var securityToken = secTokenScript;
securityToken = securityToken.Substring(0, securityToken.IndexOf(
"arrayImageTimes.push"
));
securityToken = secTokenScript.Substring(securityToken.Length).Replace(
"arrayImageTimes.push('"
,
""
).Replace(
"')"
,
""
);
var dates = securityToken.Trim().Split(
new
string
[] {
";"
}, StringSplitOptions.RemoveEmptyEntries);
var scriptDates = dates.Select(x =>
new
ScriptDate { DateString = x });
foreach
(var date
in
scriptDates)
{
string
img =
"https://en.sat24.com/image?type=infraPolair®ion=tu×tamp="
+ date.DateString;
Image image = DownloadImageFromUrl(img);
image.Save(@
"d:\satimages\" + counter + "
.jpg");
counter++;
}
}
public
class
ScriptDate
{
public
string
DateString {
get
;
set
; }
public
int
Year
{
get
{
return
Convert.ToInt32(
this
.DateString.Substring(0, 4));
}
}
public
int
Month
{
get
{
return
Convert.ToInt32(
this
.DateString.Substring(4, 2));
}
}
public
int
Day
{
get
{
return
Convert.ToInt32(
this
.DateString.Substring(6, 2));
}
}
public
int
Hours
{
get
{
return
Convert.ToInt32(
this
.DateString.Substring(8, 2));
}
}
public
int
Minutes
{
get
{
return
Convert.ToInt32(
this
.DateString.Substring(10, 2));
}
}
}
public
System.Drawing.Image DownloadImageFromUrl(
string
imageUrl)
{
System.Drawing.Image image =
null
;
try
{
System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
webRequest.AllowWriteStreamBuffering =
true
;
webRequest.Timeout = 30000;
System.Net.WebResponse webResponse = webRequest.GetResponse();
System.IO.Stream stream = webResponse.GetResponseStream();
image = System.Drawing.Image.FromStream(stream);
webResponse.Close();
}
catch
(Exception ex)
{
return
null
;
}
return
image;
}
private
void
backgroundWorker1_DoWork(
object
sender, DoWorkEventArgs e)
{
Download();
}
}
}
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.
Reply
Answers (
1
)
how to implement keyboard shortcuts in a Window Form?
How To pass role information from Windows forms to its reference dlls.