Use webbrowser to save a webpage as an image

Source: Internet
Author: User

Save the web page as an image. Remember that this feature was first found in the "Window of the world" browser, As long ago. I never knew how it works.

It's okay to go to work today. I suddenly thought of this problem, so I typed "webbrowser1." in Vs and then I went to see one by one what are the best methods. After a while, I found a more attractive word"Drawtobitmap", Just try it.

Discovery is a method of drawing controls into images. Suddenly realized that this can be used to save webpages as images. So I studied it and came to the conclusion that it is so easy to save it as an image.

 

Step 1:

First, put a Textbox, button, and webbrowser control on the form.

 This  . Button1  =     New  System. Windows. Forms. Button ();
This . Textbox1 = New System. Windows. Forms. Textbox ();
This . Webbrowser1 = New System. Windows. Forms. webbrowser ();

Step 2:

Add button event

 
VoidButton1click (ObjectSender, eventargs E)
{
Webbrowser1.navigate (textbox1.text );
}

Step 3:

After the webbrowser control loads the webpage, execute this method to start plotting our graph.

 Void  Webbrowser1documentcompleted (  Object  Sender, webbrowserdocumentcompletedeventargs E)
{
Size mysize = Webbrowser1.document. Window. size;
Bitmap mypic = New Bitmap (mysize. Width, mysize. Height );
Rectangle myrec = New Rectangle ( 0 , 0 , Mysize. Width, mysize. Height );
Webbrowser1.size = Mysize;
Webbrowser1.drawtobitmap (mypic, myrec );
Mypic. Save ( " C: \ aaaaaa.jpg " );
MessageBox. Show ( " OK " );

}

Here is an explanation:

1: First LineCode: Obtain the size of the opened webpage.

2: create an image with the size of the webpage content.

3: Create a rectangle;

4: Set the webbrowser size to display the entire page.

5: Use the drawtobitmap function to draw a webbrowser control as an image.

6. Save the image.

7: the prompt is OK.

--------------------

It is so simple that the function of saving a webpage as an image is OK. Of course, we recommend that you hide the webbrowser control to make it more beautiful.

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.