title: Taking Screenshot of Web Page (with Silverlight Controls) Programatically
body: <p>I have a web page that displays some silver light controls. I need to take screenshot of this web page programmatically.</p>
  
  <p>Currently using the System.Windows.Forms.WebBrowser control for taking screenshot.</p>
  
  <p>Forms.WebBrowser works fine when I take screenshot for normal pages. However for the pages with Silverlight controls it does not work.</p>
  
  <p>My code for taking screenshot is as follows:
              Bitmap bitmap = null;
              using (WebBrowser webBrowser = new WebBrowser())
              {
                  webBrowser.ScrollBarsEnabled = false;
                  webBrowser.ScriptErrorsSuppressed = true;</p>
  
  <pre><code>            // Set the size of the WebBrowser control
              webBrowser.Width = width;
              webBrowser.Height = height;
  
              // Load the webpage into a WebBrowser control
              webBrowser.Navigate(url);
              while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
              {
                  Application.DoEvents();
              }
  
              if (width == -1)
              {
                  // Take Screenshot of the web pages full width
                  webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width;
              }
  
              if (height == -1)
              {
                  // Take Screenshot of the web pages full height
                  webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height;
  
              }
  
              // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
              bitmap = new Bitmap(webBrowser.Width, webBrowser.Height);
              webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser.Width, webBrowser.Height));
  </code></pre>
  
  <p>}</p>