示例#1
0
        /// <summary>
        /// 把内容中的图片,相对地址变为绝对地址。
        /// </summary>
        /// <param name="tmp_content"></param>
        /// <param name="url"></param>
        public static void ImageSrc(ref string tmp_content, string url)
        {
            //如果遇到使用绝对路径的图片,转换为全路径。
            NodeList htmlNodes = new Parser(new Lexer(tmp_content.Replace("<IMG", "<img").Replace("<Img", "<img"))).Parse(new TagNameFilter("img"));

            for (int j = htmlNodes.Count - 1; j >= 0; j--)
            {
                ImageTag link = (ImageTag)htmlNodes.ElementAt(j);

                string urlpart = link.GetAttribute("src");

                if (!string.IsNullOrEmpty(urlpart) && !new Regex(@"^http:").IsMatch(urlpart))
                {
                    urlpart = new xkHttp().getDealUrl(url, urlpart);
                    string oldlink = link.ToHtml();

                    link.RemoveAttribute("src");
                    link.RemoveAttribute("onclick");
                    string newsrc  = "src=\"" + urlpart + "\" ";
                    string newlink = link.ToHtml();
                    newlink     = newlink.Insert(5, newsrc);
                    tmp_content = tmp_content.Replace(oldlink, newlink);
                    //EchoHelper.Echo("成功转换了一个图片的SRC属性!", "", EchoHelper.EchoType.普通信息);
                }
                if (!string.IsNullOrEmpty(urlpart) && new Regex(@"^\.\.").IsMatch(urlpart))
                {
                    string oldlink = link.ToHtml();
                    tmp_content = tmp_content.Replace(oldlink, "");
                }
            }
        }