-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
155 lines (144 loc) · 5.56 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using mshtml;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Threading;
namespace Read_feed
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch sw = new Stopwatch();
WebClient client = new WebClient();
int countdowmimg = 0,futt=0;
private ManualResetEvent mre = new ManualResetEvent(true);
private void Form1_Load(object sender, EventArgs e)
{
//ตั้งค่าแบ็คกราวรัน
bgwork2.WorkerReportsProgress = true;
bgwork2.DoWork += new DoWorkEventHandler(bgwork2_DoWork);
bgwork2.ProgressChanged += new ProgressChangedEventHandler(bgwork2_Progresschanged);
bgwork2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwork2_RunWorkerCompleted);
}
private void button1_Click(object sender, EventArgs e)
{
retrievHTML();
futt = 0;
countdowmimg = 0;
}
private void reimg(String htmlContent)
{
int countimgwithout=0;
int countimg = 0;
string imgsouce;
string urlfilter = text_urlfilter.Text;
string regg = "*";
string allurl = String.Concat(urlfilter, regg); //ต่อข้อความ
IHTMLDocument2 htmlDocment = (IHTMLDocument2)new mshtml.HTMLDocument();
htmlDocment.write(htmlContent);
listBox1.Items.Clear();
IHTMLElementCollection imgElements = htmlDocment.images;
foreach (IHTMLImgElement img in imgElements) // วนดึงรูปภาพจนครบ
{
imgsouce = img.src;
Match match = Regex.Match(imgsouce, allurl, RegexOptions.IgnoreCase);
countimgwithout = countimgwithout + 1;
if(match.Success)
{
listBox1.Items.Add(imgsouce);
countimg = countimg + 1;
}
}
label5.Text = Convert.ToString(countimgwithout);
label6.Text = Convert.ToString(countimg);
}
private void retrievHTML()
{
Stream data = client.OpenRead(new Uri(texturl.Text));
StreamReader reader = new StreamReader(data);
String htmlContent = reader.ReadToEnd();
reimg(htmlContent);
data.Close();
reader.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (!bgwork2.IsBusy)
bgwork2.RunWorkerAsync();
else
MessageBox.Show("Can't run the worker twice!");
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//label12.Text = string.Format("{0} ", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
/*label12.BeginInvoke(new Action(() =>
{
label12.Text = string.Format("{0} ", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
}
));*/
bgwork2.ReportProgress(e.ProgressPercentage);
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
mre.Set();
}
private void bgwork2_DoWork(object sender, DoWorkEventArgs e)
{
string pathString = @"E:\mangas\" + textBox2.Text;
System.IO.Directory.CreateDirectory(pathString);
string subfolder = System.IO.Path.Combine(pathString, textBox1.Text);
System.IO.Directory.CreateDirectory(subfolder);
subfolder = subfolder + "\\";
if (futt == 0)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
String urlimg = listBox1.Items[i].ToString();
String urlsource = subfolder + Convert.ToString(i) + ".jpg";
client.DownloadFileAsync(new Uri(urlimg), urlsource);
//bgwork2.ReportProgress(0, null);
sw.Start();
futt = 1;
countdowmimg += 1;
while (client.IsBusy)
{
System.Threading.Thread.Sleep(1);
}
}
}
}
private void bgwork2_Progresschanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.BeginInvoke(new Action(() =>
{
progressBar1.Value = e.ProgressPercentage;
}
));
label12.BeginInvoke(new Action(() =>
{
label12.Text = e.ProgressPercentage.ToString();
label10.Text = Convert.ToString(countdowmimg);
}
));
}
private void bgwork2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("เสร็จแล้ว");
}
}
}