抓取图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
namespace ImgSnatch
{
class Program
{
static void Main(string[] args)
{
Pic_Remote("<img src=http://www.google.cn/images/nav_logo4.png>");
}
private static string Pic_Remote(string news_Content)
{
string htmlStr = news_Content;
string nowyymm = DateTime.Now.ToString("yyyy-MM"); //当前年月
string nowdd = DateTime.Now.ToString("dd"); //当天号数
string path=string.Format(@"d:\imgSnatch\images\{0}\{1}",nowyymm,nowdd);
Directory.CreateDirectory(path);
string returnValue = "";
returnValue = SaveUrlPics(htmlStr, path, nowyymm, nowdd);
return returnValue;
}
//下载图片到本地
public static string SaveUrlPics(string strHTML, string path, string nowyymm, string nowdd)
{
string[] imgurlAry = GetImgTag(strHTML);
try
{
for (int i = 0; i < imgurlAry.Length; i++)
{
//WebRequest req = WebRequest.Create(imgurlAry[i]);
string preStr = System.DateTime.Now.ToString() + "_";
preStr = preStr.Replace("-", "");
preStr = preStr.Replace(":", "");
preStr = preStr.Replace(" ", "");
WebClient wc = new WebClient();
wc.DownloadFile(imgurlAry[i], path + "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
//替换原图片地址
string imgPath = "/Files/Remoteupfile/" + nowyymm + "/" + nowdd;
strHTML = strHTML.Replace(imgurlAry[i], imgPath + "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
}
}
catch (Exception ex)
{
//return ex.Message;
}
return strHTML;
}
//获取图片标志
private static string[] GetImgTag(string htmlStr)
{
Regex regObj = new Regex("<img.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string[] strAry = new string[regObj.Matches(htmlStr).Count];
int i = 0;
foreach (Match matchItem in regObj.Matches(htmlStr))
{
strAry[i] = GetImgUrl(matchItem.Value);
i++;
}
return strAry;
}
//获取图片URL地址
private static string GetImgUrl(string imgTagStr)
{
string str = "";
Regex regObj = new Regex("http://.+.(?:jpg|gif|bmp|png)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match matchItem in regObj.Matches(imgTagStr))
{
str = matchItem.Value;
}
return str;
}
}
}