Dynamically Adding Height & Width to Image Source in ASP.NET

Overview

Recently, I was working on a project in which there was a scenario in which I had to add height and width of an image to its source.

I have done it by two methods which are given below:

First Method

In this method, I have the used the jQuery. Following is my scenario:

I have the following html code taken from texteditor:

  1. <p>Hi all<strong>This is nimit</strong>,</p>  
  2. <p><img src="http://localhost:27014/uploads/Images/download.jpg" alt="" /></p>  
  3. <p>come</p>  
  4. <p><img src="http://localhost:27014/uploads/Images/Keynote.jpg." alt="" /></p>  
  5. <p>Thanks</p>  

Now I have to change the source of image and add height and width to its source with handler. For that, the following jQuery code is useful and easy:

  1. var img = tinymce.get('my_editor').getContent();  
  2. var ImageMessage = tinymce.get('my_editor').getContent();  
  3.   
  4. var srcUrl;  
  5.   
  6. $(img).find('img').addBack().filter('img').each(function () {  
  7.     var newSrc = jQuery(this).attr('src');  
  8.     srcUrl = jQuery(this).attr('src') +  
  9.        '.ashx?width=' +  
  10.     150 +  
  11.     '&height=' + 150;  
  12.     jQuery(this).attr('src', srcUrl);  
  13.     ImageMessageImageMessage = ImageMessage.replace(newSrc, srcUrl);  
  14. });  

Now “ImageMessage” have the refined source html.

Second Method

In this method, I have installed the “HtmlAgilityPack” from Package Manager Console from the following code:

Install-Package HtmlAgilityPack -Version 1.4.8

We have following html code:

  1. string input = @"<p>Hi all<strong>This is nimit</strong>,</p><p><img src='http://localhost:27014/uploads/Images/download.jpg' alt="" /></p><p>come</p><p><img src='http://localhost:27014/uploads/Images/Keynote.jpg.' alt="" /></p><p>Thanks</p>";  

Now I have to change the source of image and add height and width to its source with handler. For that, the following csharp code is useful and easy:

  1. private string ConvertImagePathToRelative(string input)  
  2. {  
  3.     string h = string.Empty;  
  4.     var htmlDoc = new HtmlDocument();  
  5.     htmlDoc.LoadHtml(input);  
  6.   
  7.     foreach (HtmlNode node in htmlDoc.DocumentNode  
  8.                                     .SelectNodes("//img[@src and (@width or @height)]"))  
  9.     {  
  10.         var src = node.Attributes["src"].Value.Split('?');  
  11.   
  12.         node.SetAttributeValue("src",  
  13.                                 src[0] +  
  14.                                 string.Format(".ashx?width=150&height=150"));  
  15.         h = h + node.OuterHtml;  
  16.         var src1 = node.Attributes["src"].Value.ToString();  
  17.         input = input.Replace(src[0].ToString(), src1);  
  18.     }  
  19.     return input;  
  20. }  

That’s it for this blog.

Summary

So far in this blog we have jQuery and HtmlAgilityPack to add height and width to its source of any image or multiple images. Thanks for reading.