コード例 #1
0
        /// <summary>
        /// Remove image width & heigth style
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string RemoveSizeStyle(string html)
        {
            var parser   = new HtmlParser();
            var document = parser.Parse(html);

            foreach (var ImgTag in document.QuerySelectorAll("img"))
            {
                ImgTag.RemoveAttribute("width");
                ImgTag.RemoveAttribute("height");

                List <string> ActualStyle = new List <string>();
                if (!ImgTag.GetAttribute("style").IsNullOrEmpty())
                {
                    ActualStyle = ImgTag.GetAttribute("style").Split(';').ToList();;
                }

                string NewStyle = string.Empty;
                if (ActualStyle.Count > 0)
                {
                    foreach (string StyleProperty in ActualStyle)
                    {
                        if (!StyleProperty.ToLower().Contains("width") && !StyleProperty.ToLower().Contains("height"))
                        {
                            NewStyle += StyleProperty.ToLower() + ";";
                        }
                    }
                }

                ImgTag.SetAttribute("style", NewStyle);
            }

            return(document.Body.InnerHtml);
        }