private static void AdjustImages(ref string html, ConverterOptions options, string rootPath = null) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img[@src]"); string size = "full"; if (imgNodes == null || imgNodes.Count == 0) { return; } for (var i = 0; i < imgNodes.Count; i++) { HtmlNode node = imgNodes[i]; // If root path is known, check if images are too big for full size class if (rootPath != null && File.Exists(DragonUtil.GetFullFilePath(node.Attributes["src"].Value, rootPath))) { var imageSize = ImageHelper.GetDimensions(DragonUtil.GetFullFilePath(node.Attributes["src"].Value, rootPath)); if (imageSize.x > 700) { size = "large"; } else { size = "full"; } HtmlAttribute width = doc.CreateAttribute("width", imageSize.x.ToString()); node.Attributes.Add(width); HtmlAttribute height = doc.CreateAttribute("height", imageSize.y.ToString()); node.Attributes.Add(height); } if (i == 0 && options.FirstImageIsAlignedRight) // First image should be right aligned, it's the 250x250 image { HtmlAttribute classAttribute = doc.CreateAttribute("class", "alignright size-" + size); node.Attributes.Add(classAttribute); } else { HtmlAttribute classAttribute = doc.CreateAttribute("class", "aligncenter size-" + size); node.Attributes.Add(classAttribute); } if (options.AddBordersToImages) { //HtmlAttribute attr = doc.CreateAttribute("bordered"); //node.Attributes.Add(attr); node.Attributes["class"].Value += " bordered"; } } html = doc.DocumentNode.OuterHtml; }
private static void AddClassToImages(ref string html, bool firstImageRightAligned, string rootPath = null) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNodeCollection imgNodes = doc.DocumentNode.SelectNodes("//img[@src]"); string size = "full"; if (imgNodes == null || imgNodes.Count == 0) { return; } for (var i = 0; i < imgNodes.Count; i++) { HtmlNode node = imgNodes[i]; // If root path is known, check if images are too big for full size class if (rootPath != null) { var imageSize = ImageHelper.GetDimensions(DragonUtil.GetFullFilePath(node.Attributes["src"].Value, rootPath)); if (imageSize.Width > 700) { size = "large"; } else { size = "full"; } } if (i == 0 && firstImageRightAligned) // First image should be right aligned, it's the 250x250 image { HtmlAttribute classAttribute = doc.CreateAttribute("class", "alignright size-" + size); node.Attributes.Add(classAttribute); } else { HtmlAttribute classAttribute = doc.CreateAttribute("class", "aligncenter size-" + size); node.Attributes.Add(classAttribute); } } html = doc.DocumentNode.OuterHtml; }