Home>

Web page screenshots are a small requirement for many sites.This code implements how to take a screenshot of a webpage based on the url and output it to the webpage.

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.threading;
using system.windows.forms;
using system.drawing;
using system.io;
///<summary>
///this page show the way of generate a image in website
///</summary>
public partial class default2:system.web.ui.page
{
  protected void page_load (object sender, eventargs e)
  {
    bitmap m_bitmap=websitethumbnail.getwebsitethumbnail ("http://www.google.cn", 600, 600, 600, 600);
    memorystream ms=new memorystream ();
    m_bitmap.save (ms, system.drawing.imaging.imageformat.png);//jpg, gif, png, etc.
    byte [] buff=ms.toarray ();
    response.binarywrite (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 thumbnailgenerator.generatewebsitethumbnailimage ();
  }
  public bitmap generatewebsitethumbnailimage ()
  {
    thread m_thread=new thread (new threadstart (_generatewebsitethumbnailimage));
    m_thread.setapartmentstate (apartmentstate.sta);
    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!=webbrowserreadystate.complete)
      application.doevents ();
    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.bounds.width, m_webbrowser.bounds.height);
    m_webbrowser.bringtofront ();
    m_webbrowser.drawtobitmap (m_bitmap, m_webbrowser.bounds);
    m_bitmap=(bitmap) m_bitmap.getthumbnailimage (m_thumbnailwidth, m_thumbnailheight, null, intptr.zero);
  }
}
c
  • Previous JS implementation of the page jump progress bar used directly
  • Next angularjs create popup box to achieve drag effect