相聚一刻

相聚一刻

导航

远程抓取图片

Posted on 2008-12-06 16:20  相聚一刻  阅读(297)  评论(0编辑  收藏  举报

using System.Text.RegularExpressions;
using System.Net;

设用方法

Pic_Remote("<img src=http://static.tianya.cn/w250/images/20081206/6262712/1228548462160.jpg>")

 //远程存图
      private 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 = "images/" + nowyymm + "/" + nowdd;
          Directory.CreateDirectory(Server.MapPath(path));
          string returnValue = "";
          returnValue = SaveUrlPics(htmlStr, path, nowyymm, nowdd);
          return returnValue;
      }

      //下载图片到本地
      public 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], Server.MapPath(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 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 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;
      }