// 获取指定网址下的html页面中的图片文件路径列表
private static List<string> getImageUrlList(string strSource)
{
Regex reg = new Regex(@"http://\S+\.(gif|bmp|png|jpg)");
MatchCollection mc = reg.Matches(strSource);
List<string> strList = new List<string>();
for (int i = 0; i < mc.Count; i++)
{
strList.Add(String.Format("{0}\r\n", mc[i].Value));
}
List<string> newList = new List<string>();
newList.AddRange(strList.Distinct());
return newList;
}
// 获取指定url路径的图片,保存下来
private static void getUrlImage(string imageUrl)
{
try
{
int lastIndex = imageUrl.LastIndexOf('/');
string fileName = imageUrl.Substring(lastIndex + 1); // 文件名称
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream(); // 网络stream, 不可获取长度
BinaryReader reader = new BinaryReader(receiveStream);
string imagePath = @".\img\";
if (!Directory.Exists(imagePath))
Directory.CreateDirectory(imagePath);
using (FileStream stream = new FileStream( imagePath + fileName, FileMode.Create))
{
byte[] buffer = new byte[256]; // 通过缓存取得数据
int count = reader.Read(buffer, 0, 256);
while (count > 0)
{
stream.Write(buffer, 0, count);
count = reader.Read(buffer, 0, 256);
}
stream.Flush();
}
receiveStream.Close();
}
catch { }
}
// 调用示例
private void captureImageByUrl(string urlStr)
{
string strSource = getUrlHtml(urlStr);
if (strSource == String.Empty) return;
List<string> strList = getImageUrlList(strSource);
StringBuilder sb = new StringBuilder();
foreach (string item in strList)
{
sb.Append(item);
getUrlImage(item.TrimEnd(new char[] { '\r', '\n' }));
}
MessageBox.Show(sb.ToString());
}