views:

158

answers:

2

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.

A: 

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!

Christina Mayers
A: 

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();
}
WebDevHobo