SoFunction
Updated on 2025-03-07

C# implements the method of screenshotting and outputting to the web page according to the URL in a web page

This article describes the method of C# implementing screenshots and outputting them to the web page according to urls in a web page. Share it for your reference, as follows:

Web page screenshots are a small requirement for many sites. This code implements how to obtain web page screenshots based on url and output them to the web page.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
/// <summary>
/// This page show the way of generate a image in website
/// </summary>
public partial class Default2 : 
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Bitmap m_Bitmap = ("", 600, 600, 600, 600);
    MemoryStream ms = new MemoryStream();
    m_Bitmap.Save(ms, );//JPG, GIF, PNG, etc. are all available    byte[] buff = ();
    (buff);
  }
}
public class WebSiteThumbnail
{
  Bitmap m_Bitmap;
  string m_Url;
  int m_BrowserWidth, m_BrowserHeight, m_ThumbnailWidth, m_ThumbnailHeight;
  public WebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
  {
    m_Url = Url;
    m_BrowserHeight = BrowserHeight;
    m_BrowserWidth = BrowserWidth;
    m_ThumbnailWidth = ThumbnailWidth;
    m_ThumbnailHeight = ThumbnailHeight;
  }
  public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
  {
    WebSiteThumbnail thumbnailGenerator = new WebSiteThumbnail(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight);
    return ();
  }
  public Bitmap GenerateWebSiteThumbnailImage()
  {
    Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
    m_thread.SetApartmentState();
    m_thread.Start();
    m_thread.Join();
    return m_Bitmap;
  }
  private void _GenerateWebSiteThumbnailImage()
  {
    WebBrowser m_WebBrowser = new WebBrowser();
    m_WebBrowser.ScrollBarsEnabled = false;
    m_WebBrowser.Navigate(m_Url);
    m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
    while(m_WebBrowser.ReadyState != )
      ();
    m_WebBrowser.Dispose();
  }
  private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
    WebBrowser m_WebBrowser = (WebBrowser)sender;
    m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
    m_WebBrowser.ScrollBarsEnabled = false;
    m_Bitmap = new Bitmap(m_WebBrowser., m_WebBrowser.);
    m_WebBrowser.BringToFront();
    m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
    m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, );
  }
}

I hope this article will be helpful to everyone's C# programming.