Iterate through an html string to find all img tags and replace the src attribute values
<p>I have an html code as a string. I need to find all img tags in that string, read the value of each src attribute and pass it to a function, that function returns an entire img tag that needs to take the place of the img tag that was read. </p>

<p>It needs to iterate through the whole string and execute the same  logic for all img tags.</p>

<p>For example, suppose that my html string looks like this:</p>

<pre><code>string htmlBody= "&lt;p&gt;Hi everyone&lt;/p&gt;&lt;img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAA..." &lt;p&gt;I am here &lt;/p&gt; &lt;img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABAC..." /&gt;"
</code></pre>

<p>I have the following code which finds the first img tag, takes the src value (which is a base64 string)  and convert it into an array of bits to create an stream, then i can create a new src value which link to that stream.</p>

<pre><code>  //Remove from all src attributes "data:image/png;base64"      
  string res = Regex.Replace(htmlBody, "data:image\\/\\w+\\;base64\\,", "");
  //Match the img tag and get the base64  string value
  string matchString = Regex.Match(res, "&lt;img.+?src=[\"'](.+?)[\"'].*?&gt;", RegexOptions.IgnoreCase).Groups[1].Value;
  var imageData = Convert.FromBase64String(matchString);
  var contentId = Guid.NewGuid().ToString();
  LinkedResource inline = new LinkedResource(new MemoryStream(imageData), "image/jpeg");
  inline.ContentId = contentId;
  inline.TransferEncoding = TransferEncoding.Base64;
  //Replace all img tags with the new img tag 
  htmlBody = Regex.Replace(htmlBody, "&lt;img.+?src=[\"'](.+?)[\"'].*?&gt;", @"&lt;img src='cid:" + inline.ContentId + @"'/&gt;");
</code></pre>

<p>As you can see finnaly i have got the new img tag to replace: </p>

<pre><code>   &lt;img src='cid:" + inline.ContentId + @"'/&gt;
</code></pre>

<p>But the code will replace all the img tag with the same content. I need to be able to get the img tag, execute the logic, replace it and then, continue with the next img tag.</p>

<p>Hope you can give me an idea how i can do that. Thanks in advance.</p>