Using in HtmlDocument Class in Asp.net/SharePoint -Visual Web Part
I faced this situation where I have to read a HTML from a string, get the image tag manipulate and create a new image tag and return.
I tried to use HtmlDocument object from System.Window.Form, but it caused threading issue, after few googling got reference to the HTMLAgilityPack
Downloaded and added to my project. Below is the usage.
#region Get Image From HTML Text /// <summary> /// Get image tag from html text /// </summary> /// <param name="strHTML"></param> /// <returns></returns> public static string GetImageFromHTMLText(string strHTML) { //local varibales to store the image attributes string strSrc = string.Empty; string strWidth = string.Empty; string strHeight = string.Empty; string strAlt = string.Empty; string strFinalImageText = string.Empty; try { //Using HTML Agility Pack downloaded from Codeplex - htmlagilitypack.codeplex.com //reduces the thread handling required if we use System.Windows.Form for HtmlDocument object HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlDocument(); //Loads the html htmlDoc.LoadHtml(strHTML); //foreach (HtmlNode img in doc.DocumentNode.SelectNodes("//img[@src]")) //loop thru the image tags - xsl for image - foreach (HtmlNode image in htmlDoc.DocumentNode.SelectNodes("//img")) { HtmlAttributeCollection attCollection = image.Attributes; strSrc = image.GetAttributeValue("src", null); strWidth = (image.GetAttributeValue("width", null) == null) ? "100" : image.GetAttributeValue("width", null); strHeight = (image.GetAttributeValue("height", null) == null) ? "100" : image.GetAttributeValue("height", null); strAlt = image.GetAttributeValue("alt", null); }
//Get the intended size - here as 200px Size NewSize = GetResizedImageDimensions(int.Parse(strWidth), int.Parse(strHeight), 200, 160); //Creating the new image html string strFinalImageText = string.Format(@"<img width='{0}' height='{1}' alt='{2}', src='{3}'/>", NewSize.Width, NewSize.Height, strAlt, strSrc); } catch (Exception ex) { PortalLog.LogString("Exception occrured in Get Image innter HTML" + ex.Message); } return strFinalImageText; } #endregion
|
Thanks for reading..
If you have a more elegant solution/suggestion – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg
Partha (Aurum)
No comments:
Post a Comment