遇到的难题:如何用程序实现把网页中的图片自动保存到本地

解决方案 »

  1.   

    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;
          }[/code]
      

  2.   

    http://topic.csdn.net/u/20071128/20/c4316e92-5040-49db-959c-a6e17c79ea35.html
    http://download.csdn.net/source/3181784
    http://topic.csdn.net/u/20070315/20/bd16129c-72f7-4820-8094-6a342567871b.html
    http://download.csdn.net/source/1231101/
    http://download.csdn.net/down/2935109/langmanlaowo