|
|
|
1 |
public class MyImgObj |
|
2 |
{ |
|
3 |
public string SrcUrl; |
|
4 |
public string ImgSavePath; |
|
5 |
} |
|
6 |
private void button1_Click(object sender, EventArgs e) |
|
7 |
{ |
|
8 |
|
|
9 |
|
|
10 |
WebClient wc = new WebClient(); |
|
11 |
byte[] aryByte = wc.DownloadData("http://www.blueshop.com.tw/board/FUM20050124192253INM/BRD20100129155902S4A.html"); |
|
12 |
MemoryStream ms = new MemoryStream(aryByte); |
|
13 |
string html = string.Empty; |
|
14 |
using (StreamReader sr = new StreamReader(ms, Encoding.Default)) |
|
15 |
{ |
|
16 |
html = sr.ReadToEnd(); |
|
17 |
} |
|
18 |
|
|
19 |
List<MyImgObj> listMyImgObj = new List<MyImgObj>(); |
|
20 |
FolderBrowserDialog folderBsd = new FolderBrowserDialog(); |
|
21 |
if (folderBsd.ShowDialog() == DialogResult.OK) |
|
22 |
{ |
|
23 |
string urlRoot = "http://www.blueshop.com.tw"; |
|
24 |
Regex regex = new Regex(@"<(.|\n)*?>"); |
|
25 |
Regex regexImgSrc = new Regex("src=\"[^\"]*\""); |
|
26 |
|
|
27 |
List<string> listTag = new List<string>(); |
|
28 |
List<string> listImgUrl = new List<string>(); |
|
29 |
foreach (Match m in regex.Matches(html)) |
|
30 |
{ |
|
31 |
|
|
32 |
string tag = m.Value; |
|
33 |
tag = tag.Replace("'", "\""); |
|
34 |
|
|
35 |
if (tag.ToLower().IndexOf("<img") != -1) |
|
36 |
{ |
|
37 |
if (!listTag.Contains(tag)) |
|
38 |
{ |
|
39 |
listTag.Add(tag); |
|
40 |
if (regexImgSrc.IsMatch(tag)) |
|
41 |
{ |
|
42 |
MyImgObj myImgObj = new MyImgObj(); |
|
43 |
string imgSrc = regexImgSrc.Match(tag).Value.Replace("src=\"", String.Empty).Replace("\"", String.Empty); |
|
44 |
myImgObj.SrcUrl = urlRoot + imgSrc; |
|
45 |
myImgObj.ImgSavePath = folderBsd.SelectedPath + "\\" + imgSrc.Substring(imgSrc.LastIndexOf("/") + 1); |
|
46 |
if (myImgObj.ImgSavePath.IndexOf(".") == -1) { myImgObj.ImgSavePath = myImgObj.ImgSavePath + ".jpg"; } |
|
47 |
listMyImgObj.Add(myImgObj); |
|
48 |
} |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
|
|
56 |
foreach (MyImgObj myImgObj in listMyImgObj) |
|
57 |
{ |
|
58 |
try |
|
59 |
{ |
|
60 |
ms = new MemoryStream(wc.DownloadData(myImgObj.SrcUrl)); |
|
61 |
Image img = Image.FromStream(ms); |
|
62 |
img.Save(myImgObj.ImgSavePath); |
|
63 |
} |
|
64 |
catch (Exception ex) |
|
65 |
{ |
|
66 |
|
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
MessageBox.Show("讀取完成!"); |
|
71 |
|
|
72 |
} |