利用C# 保存图片

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值