public static string DowImg(string content1, string key)
    {
        string content = content1;
        MatchCollection m = getregduo("<img.+?>", content);
        foreach (Match item in m)
        {//

            string imgurl = getreg("src=\"(.+?)\"", item.Value);//原图片url地址
            if (imgurl.IndexOf("lkjfds")!=-1)
            {
                string ddd = "ddd";
            }
  string houzui = imgurl.Substring(imgurl.LastIndexOf("."));//图片的后缀
          
            string imgname = Guid.NewGuid().ToString() + houzui;
            string name = "http://www.lkjfds.com/taoimg/" + key + "/" + imgname;//改后的图片路径;
          
          
       
            try
            {  
                DAL.Database.ExecProc("insert into img(url,houurl) values('" + imgurl + "','"+name+"')");//把已有的链接放入数据库
                content = content.Replace(imgurl, name);
            }
            catch (Exception e)
            {
                if (e.ToString().IndexOf("索引") != -1)//说明这幅图片已经存在于数据库了
                {
                    name = DAL.Database.ReturnScalar("select houurl from img where url='" + imgurl + "'").ToString();
                    content = content.Replace(imgurl, name);
                    continue;
                }
                throw;
            }
           // 如果不是就下载图片
            Stream ss = httpweb.getGet(imgurl, new System.Net.CookieContainer());//下载图片
            Image img = Image.FromStream(ss);
            img.Save(Environment.CurrentDirectory + "/" + key + "/" + imgname);//保存图片
        }
        return content;
    }

posted on 2010-08-10 09:22  黛玉  阅读(585)  评论(0编辑  收藏  举报