简体   繁体   中英

How to save a complete webpage using the built-in webbrowser in c#

Overall I am trying to write out a webpage to PDF. There is a web service that I can use to convert a file to pdf. So what I am trying to do is save out a webpage from the WebBrowser winforms control.

I have already tried writing it out the document stream but that just gives me the html of the page and not the images that are used with it.

Another way that I looked into, but have not been successful with, is trying to create an image of the WebBrowser document. I found some examples on the web that utilize the DrawToBitmap function but none of them have worked for me.

Any assistance would be grateful.

You can take screenshots until you have the entire page using the Graphics.CopyFromScreen function.

// Get screen location of web browser
Rectangle rec = webBrowser1.RectangleToScreen(webBrowser1.ClientRectangle);
// create image to hold whats in view
Bitmap image = new Bitmap(rec.Width, rec.Height);
// get graphics to draw on image
Graphics g = Graphics.FromImage(image);
// Save into image
// From MSDN:
//public void CopyFromScreen(
//    int sourceX,
//    int sourceY,
//    int destinationX,
//    int destinationY,
//    Size blockRegionSize
//)
g.CopyFromScreen(rec.X,rec.Y,0,0,rec.Size)

You may also want to remove the scrollbars so they aren't in your image:

webBrowser.ScrollBarsEnabled = false;
webBrowser.Document.Body.Style = "overflow:hidden;";

And then scroll down to take a shot of the next page:

webBrowser.Document.Window.ScrollTo(x,y);

A long time ago I stumbled over this CodeProject-article ' Capture an HTML document as an image '

however, there is a new one (posted: 13 Feb 2010) ' HTML to Image in C# '

I haven't tested either of them but I think they should work!

To create the PDF, the program you're using will need the source code of the site. Wether you use the WebBrowser winforms control or something else to get that info, is of no real difference.

This code will get the source code of any site for you, presuming you don't need to upload stuff first:

string url = "some site";
string source = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using(StreamReader sr = new StreamReader(response.GetResponseStream()){
    source = sr.ReadToEnd();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM