代碼如下
using System;
using System
using System
using System
using System
using System
using System
using System
namespace WindowsFormsApplication
{
public class GatherPic
{
private string savePath;
private string getUrl;
private WebBrowser wb;
private int iImgCount;
//初始化參數
public GatherPic(string sWebUrl
{
this
this
}
//開始采集
public bool start()
{
if (getUrl
{
MessageBox
return false;
}
this
this
//委托事件
this
return true;
}
//WebBrowser
private void DocumentCompleted(object sender
{
//頁面裡框架iframe加載完成不掉用SearchImgList()
if (e
SearchImgList()
}
//檢查出所有圖片並采集到本地
public void SearchImgList()
{
string sImgUrl;
//取得所有圖片地址
HtmlElementCollection elemColl = this
this
foreach (HtmlElement elem in elemColl)
{
sImgUrl = elem
//調用保存遠程圖片函數
SaveImageFromWeb(sImgUrl
}
}
//保存遠程圖片函數
public int SaveImageFromWeb(string imgUrl
{
string imgName = imgUrl
path = path +
string defaultType =
string[] imgTypes = new string[] {
string imgType = imgUrl
foreach (string it in imgTypes)
{
if (imgType
break;
if (it
imgType = defaultType;
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest
request
request
WebResponse response = request
Stream stream = response
if (response
{
byte[] arrayByte = new byte[
int imgLong = (int)response
int l =
// CreateDirectory(path)
FileStream fso = new FileStream(path
while (l < imgLong)
{
int i = stream
fso
l += i;
}
fso
stream
response
return
}
else
{
return
}
}
catch (WebException)
{
return
}
catch (UriFormatException)
{
return
}
}
}
}
//
GatherPic gatherpic = new GatherPic(
//請確保c:\下存在test路徑
gatherpic
From:http://tw.wingwit.com/Article/program/net/201311/11544.html