OneNoteConversionTool.FormatReaders.EpubReader.AddImageDimenssionsToHtml C# (CSharp) Method

AddImageDimenssionsToHtml() private method

Add the dimenssions of images to the Html ensuring that the images has max width of 960 pixels
private AddImageDimenssionsToHtml ( HtmlAgilityPack.HtmlDocument htmlDoc ) : void
htmlDoc HtmlAgilityPack.HtmlDocument
return void
		private void AddImageDimenssionsToHtml(HtmlDocument htmlDoc)
		{
			HtmlNodeCollection imgNodes = htmlDoc.DocumentNode.SelectNodes("//img[@src]");
			if (imgNodes == null)
			{
				return;
			}
			foreach (HtmlNode imgNode in imgNodes)
			{
				Image img;
				try
				{
					img = Image.FromFile(imgNode.Attributes["src"].Value);
				}
				catch (Exception)
				{
					Console.WriteLine("Error in AddImageDimenssionsToHtml: No image found at {0}", imgNode.Attributes["src"].Value);
					continue;
				}

				HtmlAttribute heightAttr = htmlDoc.CreateAttribute("height");
				HtmlAttribute widthAttr = htmlDoc.CreateAttribute("width");
				if (img.Width > MaxWidth)
				{
					heightAttr.Value = ((img.Height * MaxWidth) / (img.Width)).ToString(CultureInfo.CurrentCulture);
					widthAttr.Value = MaxWidth.ToString(CultureInfo.CurrentCulture);
				}
				else
				{
					heightAttr.Value = img.Height.ToString();
					widthAttr.Value = img.Width.ToString();
				}
				img.Dispose();

				imgNode.Attributes.Add(heightAttr);
				imgNode.Attributes.Add(widthAttr);
			}
		}
		#endregion