Use ASP. NET for website screenshots (sample code)

Source: Internet
Author: User

1. Add reference

Right-click the solution, select "add reference...", and add "system. Windows. Forms". After adding the solution, Web. config should have something similar to the following:

<System. Web>
<Compilation DEBUG = "true">
<Assemblies>
<Add Assembly = "system. Windows. Forms, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089"/>

Ii. Set the sta Mode

Add aspcompat = "true" to the @ page command to force the web page to be executed in Sta (single-thread unit) mode. The results are as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" aspcompat = "true" %>

Iii. WriteCode

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. drawing;
Using system. Drawing. imaging;
Using system. Windows. forms;

Public partial class _ default: system. Web. UI. Page
{
Private webbrowser _ webbrowser;


Protected void page_load (Object sender, eventargs E)
{
String url = request. querystring ["url"];
If (string. isnullorempty (URL ))
{
Url = "http://www.cftea.com /";
}

_ Webbrowser = new webbrowser ();
_ Webbrowser. scrollbarsenabled = false; // No scroll bar is displayed.
_ Webbrowser. navigate (URL );
_ Webbrowser. documentcompleted + = new webbrowserdocumentcompletedeventhandler (completed );

While (_ webbrowser. readystate! = Webbrowserreadystate. Complete)
{
System. Windows. Forms. application. doevents (); // prevents false positives. If this parameter is removed, the documentcompleted event may not be triggered.
}
}


Public void completed (Object sender, webbrowserdocumentcompletedeventargs E)
{
// Set the width and height of the browser to the width and height of the document to capture the entire webpage.
_ Webbrowser. width = _ webbrowser. Document. Body. scrollrectangle. width;
_ Webbrowser. Height = _ webbrowser. Document. Body. scrollrectangle. height;
Using (bitmap BMP = new Bitmap (_ webbrowser. Width, _ webbrowser. Height ))
{
_ Webbrowser. drawtobitmap (BMP, new rectangle (0, 0, BMP. Width, BMP. Height ));
BMP. Save ("C: \ capture.png", imageformat. PNG );
}
}
}

Iv. Description

If an error occurs during debugging, asp should be stopped. NET development server. Run it again. If you are using IIS, you may need to restart IIS (which is probably the case). Otherwise, the navigate statement will fail.

Some websites are blank and have not carefully studied why.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.