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.