利用C# 保存图片

论坛 期权论坛 脚本     
匿名技术用户   2020-12-29 03:36   38   0
hihi小弟也好寄研究了一下
一般如果找網頁圖片,好像用flashget或是直接網頁另存(kkman),就可把網頁資料下載下來
透過程式,比較常用的作法是
讀取網頁
解析html,取得img的Tag
然後將img下載到你要的目錄下
下面是簡單寫的範例,臨時寫的,可能很多問題(謹供參考)
參考看看喔^^
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 //讀取網頁 html
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)*?>"); //取 HTML tag
25 Regex regexImgSrc = new Regex("src=\"[^\"]*\""); //取 img 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 //存取所有 img 的src
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 //MessageBox.Show(ex.Message + myImgObj.SrcUrl);
67 }
68 }
69
70 MessageBox.Show("讀取完成!");
71
72 }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP