C# 抓去网站图片

没有加入线程 
/// <summary>
/// 提取图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, EventArgs e)
{
WebClient client
= new WebClient();
client.Encoding
= Encoding.UTF8;
string str = client.DownloadString(txtHTML.Text.Trim());
MatchCollection mc
= Regex.Matches(str, "<img.+?src=(['\"]?)([^>\\s]+)\\1.*?>");
int index = 0;
foreach (Match item in mc)
{
string imgpath = item.Groups[2].Value;
lbImage.Text
= imgpath;
if (imgpath.IndexOf("http:") > -1)
{
client.DownloadFile(imgpath,
@"D:\Imges\" + DateTime.Now.Millisecond + index + ".jpg");
}
else
{
imgpath
= txtHTML.Text.Trim() + imgpath;
client.DownloadFile(imgpath,
@"D:\Imges\" + DateTime.Now.Millisecond + index + ".jpg");
}
index
++;
}
MessageBox.Show(
"OK!");
}
posted @ 2011-05-31 16:33  SharonWang  阅读(167)  评论(0编辑  收藏  举报