Taking Screenshot of a Webpage Programmatically

Programmatically get a screenshot of a page

https://screenshotlayer.com/documentation is the only free service I can find lately...

You'll need to use HttpWebRequest to download the binary of the image. See the provided url above for details.

HttpWebRequest request = HttpWebRequest.Create("https://[url]") as HttpWebRequest;
Bitmap bitmap;
using (Stream stream = request.GetResponse().GetResponseStream())
{
bitmap = new Bitmap(stream);
}
// now that you have a bitmap, you can do what you need to do...

Taking screenshot of a webpage programmatically

I searched and searched and searched and found it Webpage thumbnailer (a The Code Project article).

Capture screenshot of browser content (website)

Do you know about Selenium?
It's a testing tool that actually opens a specific browser and runs scripted tests.
It can be used to take screenshots as well.

This might be a solution to your problem.

Take a screenshot of a web page in Java

To build on two of the answers above:

Rendering the HTML in Java then saving to an image - A few Java based HTML renders exist, all with different sets of drawbacks. The most common is the one built in. This is quite simple and can only render fairly basic HTML. The most intresting I know of is The Flying Saucer Project. This can render fairly complex XHTML but you will have to convert HTML before you can use it (JTindy may be able to help here). Taking a Swing component and creating a image is fairly simple, you just pass an BufferedImages graphics object and pass it to the Swing components paint method. Then splat that out with ImageIO.

A big advantage to this would be that the renderer would be headless. The disadvantage is that it would not be a perfect rendering and it would lack any plugins.

The second option requires you to start a web browser, work out where it is and then take a screen shot. Optionally you may also wish to strip out all the Firefox/IE/Opera/etc menus leaving you with just image. To get the dimensions of the web browser the simplest option would be to start it full screen. The other option would be to use something like JDICs browser component to include it as part of the Java application. It would then be able to specify where the HTML is being rendered on screen and then simply use Robot to create a screen shot of that area.

The big advantage to this is that it will give a perfect rendering (for a given browser). The two disadvantages is that it would require native code (or at least using a native component) and it could not be headless¹.

1) You could use a virtual frame buffer. But that is outside Java.

Take a screenshot of a webpage with JavaScript?

I have done this for an HTA by using an ActiveX control. It was pretty easy to build the control in VB6 to take the screenshot. I had to use the keybd_event API call because SendKeys can't do PrintScreen. Here's the code for that:

Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Const CaptWindow = 2

Public Sub ScreenGrab()
keybd_event &H12, 0, 0, 0
keybd_event &H2C, CaptWindow, 0, 0
keybd_event &H2C, CaptWindow, &H2, 0
keybd_event &H12, 0, &H2, 0
End Sub

That only gets you as far as getting the window to the clipboard.

Another option, if the window you want a screenshot of is an HTA would be to just use an XMLHTTPRequest to send the DOM nodes to the server, then create the screenshots server-side.



Related Topics



Leave a reply



Submit