Scroll webpage screenshot capture ultra-long webpage Image

Source: Internet
Author: User

Code written

There are still many imperfections in the program

This is the current status. The following is a website homepage that can be saved as a specified format.

 

How to Use the WebBrowser control to load the required webpage

Call the WebBrowser Navigate (string Url) method to load the required webpage.

This method also has some disadvantages. WebBrowser does not have the intelligent error correction function of IE, which may cause resolution Errors for webpages with less strict CSS styles. As a result, the layout of webpages is also chaotic.

Some websites using ajax technology may not be able to steal dynamic and delayed loading content based on their loading methods.

Some webpages (very few) cannot be processed, and the specific cause of the error is not found yet. However, most webpages can be completed.

When the WebBrowser page is loaded, adjust the size of the page and set the length to the length and width of the page. It is best to set it to a fixed width, for example, 1440 or 1024, because some web pages are the adaptive browser width of these web pages for stream layout, some div pages in the webpage Body area are fixed width, but the fixed web page width generally does not exceed 1024px

The following are the key webBrowser methods and attributes that need to be used.

Navigate (url)

WebBrowser1.Document. Body. ScrollRectangle. Height

WebBrowser1.DrawToBitmap is the most critical method. This method is described by an API function. net sdk.

DrawToBitmap Infrastructure. This method is not supported by this control. (Inherited from WebBrowserBase .)

This method is not suitable for direct use in code, but is not unusable. Many people are confused about the scrolling. NET class libraries.

We can call this method to draw a webpage to a previously created bitmap in the memory to convert and save the bitmap format.

The following fields are used:

Private string _ currentFormat = "png ";
Private int _ currentWidth = 1024;
Private string _ fileName = null;
Private Bitmap _ currentBitmap = null;

 

After loading

 

Code

1 private void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
2 {
3 if (webBrowser1.Document = null)
4 return;
5 try
6 {
7 int width = _ currentWidth;
8 int height = webBrowser1.Document. Body. ScrollRectangle. Height;
9 webBrowser1.Width = width;
10 webBrowser1.Height = height;
11 _ currentBitmap = new Bitmap (width, height );
12 webBrowser1.Stop ();
13 webBrowser1.DrawToBitmap (_ currentBitmap, new Rectangle (0, 0, width, height ));
14 pictureBox1.Image = _ currentBitmap;
15 button1.Enabled = true;
16}
17 catch (Exception ex)
18 {
19 button1.Enabled = true;
20 MessageBox. Show (ex. ToString (), "encountered Error", MessageBoxButtons. OK, MessageBoxIcon. Error );
21}
22}

 

 

This method adjusts the height of the webBrowser control to the same as that of the webpage and then draws it to the created image.

In this way, the image object _ currentBitmap can be obtained and the Save method can be called to Save the corresponding format to the disk.

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.