Question

How to use C# to capture a image of a specific url?

I want to use C# to automatically capture a image of a webpage based on a specific url.

For example, I have a page contains a txtUrl.Text = "http://www.some.com/index.aspx" , then I click a button, how can I capture a image of that Url?

Was it helpful?

Solution

I assume you want to do this from ASP.NET (as opposed to from a WinForms application).

In your web project, add a reference to System.Windows.Forms (yes, this is a bad thing to do). In your code-behind, you can then create an object of type System.Windows.Forms.WebBrowser:

WebBrowser browser = new WebBrowser();
// this will load up a URL into the web browser:
browser.Navigate(@"http://www.stackoverflow.com"); 

Next, just use the BitBlt API function (sorry, I don't have a link handy) to copy the WebBrowser control's graphical display to a Bitmap (which you can then display or save or whatever). With this function, the WebBrowser's Handle property is one of the parameters to pass.

Update: here's a link to some code that does exactly what you need: http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/

OTHER TIPS

If you mean a visual of the webpage, one approach is to integrate IE to your application and programmatically taking a screenshot. This (for the integrated web browser) and this (for taking screenshots with C#) may be of use. This is of course IE dependent.

Another option is using the shotserver and shotfactory projects used for browsershots.org. They can be found here, though I'm not sure if there's a .NET API for it.

I don't think that is really possible only using C#. That is because C#, or the .NET framework for that matter, don't offer any kind of HTML markup rendering capabilities. The closest you can get - in my opinion - would be to use a WebBrowser control and then try to somehow capture it's graphical output (which would be the rendered page).

The other way to do it would be to look for a .NET component that might do what you want.. Although I don't know of any that do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top