0

I have WebBrowser control in my WP7 app

I want to save the page in HTML or PDF or JPG file in isolated memory for read it later.

sma6871
  • 3,198
  • 3
  • 38
  • 52

1 Answers1

1

you can use a WebClient:

WebClient downloader = new WebClient();
downloader.DownloadStringCompleted += (o, e) => DoSomethingWithResult(e.Result);
downloader.DownloadStringAsync(new Uri(yourWebBrowser.Source.ToString()));

private void DoSomethingWithResult(string result)
{
    //...
}

Of course, you need to check e.Error and so on... I left that out for the sake of brevity.

In order to download the entire page, not just the HTML, you should look into this question. Be warned, it probably isn't as simple as you think.

EDIT: in order to show the HTML you saved using the above method, call WebBrowser.NavigateToString(result).
You can find an example in this blog post.

Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63
  • you said you wanted the HTML, PDF or JPG. This gives you the HTML. Edit your question and be more exact if you need help with *showing* a locally saved html file. – Adam Dec 25 '11 at 18:58
  • yes but i want complete HTML that's contains images and ... for offline view – sma6871 Dec 25 '11 at 19:00
  • @L.B note the *or*. I've edited the answer with a link to an SO question that deals with *downloading the entire page*. – Adam Dec 25 '11 at 19:03
  • @Downvoter, I'm pretty confident that I answered the question as posed by the OP, and I'm trying to accommodate what they are adding in the comments, so take a second to reconsider your vote. – Adam Dec 25 '11 at 19:07
  • is there any way to save it to JPG file? – sma6871 Dec 25 '11 at 19:11
  • @L.B in fact, it is possible, [as this blog post proves](http://blogs.msdn.com/b/mikeormond/archive/2010/12/16/displaying-html-content-in-windows-phone-7.aspx), by using the `WebBrowser.NavigateToString()` method as shown [on MSDN](http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.navigatetostring.aspx). – Adam Dec 25 '11 at 21:53
  • @L.B No, I'm sorry. I have no idea how to save it as a JPG. I was responding to your comment about how downloading the HTML would be unsuitable - as my links show, it is possible to display the downloaded html later. And the SO question I linked contains information about how to download the entire page (images, etc) - the difficulty would be about how to display the page *exactly* as it first appeared, but using the MSDN technique it is at least possible to preserve CSS and JS, leaving only the images as a problem, which could probably be worked around. – Adam Dec 25 '11 at 22:05
  • @L.B I respect your decision to downvote (while disagreeing, of course, on the grounds that your reasoning is incorrect), but I wanted to give you the chance to see that using `WebClient` to download the HTML **is** a reasonable suggestion. I promise I won't bother you with further mentions... – Adam Dec 25 '11 at 22:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6126/discussion-between-codesparkle-and-l-b) – Adam Dec 25 '11 at 22:12