title: how to render the web browser and get x y coordinates of html tags(e.f. "<img", "<div", so on...) and take a screen shot them in C#?
body: <p>I'd like to get some screenshots(as an image) that is one of many HTML elements by using C#. I just need to know about making each step.</p>
  
  <ol>
  <li>Load Internet Explorer and web URL(e.g. stackoverflow.com, so
  on...). </li>
  <li>Parse the DOM(Document Object Model) structure.</li>
  <li>Render this web page. </li>
  <li>Get the x y coordinates(or
  position information) of HTML elements that I want to get(e.f.
  "&lt;img", "&lt;div", so on...). In this section, it does not matter
  which HTML element it is. </li>
  <li>Take screenshots by using the x y
  coordinates and width, height, ....</li>
  </ol>
  
  <p>but until now, I haven't finished this project because I don't know how to, in fact, I'm not even sure these steps are correct.</p>
  
  <p>So, please tell me how can I can finish this project and if these steps are correct, please revise my information also, if you know a more efficient or correct code.</p>
  
  <p>p.s. I made this code by referencing other codes until now.</p>
  
  <pre><code>namespace ConsoleApplication2
  {
      class Program
      {
  
          public static void Main(string[] args)
          {
              string webPageURL = "http://www.google.com";
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webPageURL);
              request.Credentials = System.Net.CredentialCache.DefaultCredentials;
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
              //IWebElement element = driver.FindElement(By.Id("SubmitButton"));
              //Point point = element.Location;
              //Console.WriteLine("X cordinate : " + point.X + "Y cordinate: " + point.Y);
  
              SvgPoint sp = new SvgPoint();
              //SvgRenderer sr = new SvgRenderer();
              //SvgElement se = new SvgElement;
              SvgPointCollection spc = new SvgPointCollection();
  
              if (response.StatusCode == HttpStatusCode.OK)
              {
                  using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                  {
                      string htmlContent = sr.ReadToEnd();
                      List&lt;Uri&gt; links = FetchLinksFromSource(htmlContent);
  
                      for (int i = 0; i &lt; links.Count; i++)
                      {
                          Console.WriteLine(links[i]);
                      }
  
                      using (WebClient client = new WebClient())
                      {
                          for (int i = 0; i &lt; links.Count; i++)
                          { 
                              WebBrowser WebBrowser1 = new WebBrowser();
                              //WebBrowser1.Document.GetElementsByTagName("img");
  
                              IHTMLElement htmldoc = (IHTMLElement)new HTMLDocument();
                              //IHTMLElement htmldoc1 = (IHTMLElement)Document.body;
  
                              WebBrowser1.Navigate(webPageURL);
  
                              sp.X = findPosX(htmldoc);
                              sp.Y = findPosY(htmldoc);
  
                          }
                      }
                  }
              }
          }
  
  
          public static List&lt;Uri&gt; FetchLinksFromSource(string htmlSource)
          {
              List&lt;Uri&gt; links = new List&lt;Uri&gt;();
              string regexImgSrc = @"&lt;img[^&gt;]*?src\s*=\s*[""']?([^'"" &gt;]+?)[ '""][^&gt;]*?&gt;";
              MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
              foreach (Match m in matchesImgSrc)
              {
                  string href = m.Groups[1].Value;
                  links.Add(new Uri(href));
              }
              return links;
          }
  
  
          public static int findPosX(IHTMLElement obj)
          {
              int curleft = 0;
              if (obj.offsetParent != null)
              {
                  while (obj.offsetParent != null)
                  {
                      curleft += obj.offsetLeft;
                      obj = obj.offsetParent;
                  }
              }
  
              return curleft;
          }
  
  
          public static int findPosY(IHTMLElement obj)
          {
              int curtop = 0;
              if (obj.offsetParent != null)
              {
                  while (obj.offsetParent != null)
                  {
                      curtop += obj.offsetTop;
                      obj = obj.offsetParent;
                  }
              }
  
              return curtop;
          }
      }
  }
  </code></pre>