I was recently asked to come up with some Java code that would convert Web pages into images.
My objective was to have the Web page become an image upon the clicking of a button or when certain events happened. Below is the Java code I came up with, I hope it works as well for you as it did for me.
//Call the Web page and convert to Image
BufferedImage ire;
ire = WebImage.create("<web page URL>", 800, 600);
//You can convert the BufferedImage to
any format that you wish, jpg I thought was the best format
ImageIO.write(ire, "jpg", new File
("c:\\Temp\\tt.jpg"));
//Class that Converts the web page to Image
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public abstract class WebImage
{
static class Kit extends HTMLEditorKit
{
public Document createDefaultDocument() {
HTMLDocument doc =
(HTMLDocument) super.createDefaultDocument();
doc.setTokenThreshold(Integer.MAX_VALUE);
doc.setAsynchronousLoadPriority(-1);
return doc;
}
}
public static BufferedImage create
(String src, int width, int height) {
BufferedImage image = null;
JEditorPane pane = new JEditorPane();
Kit kit = new Kit();
pane.setEditorKit(kit);
pane.setEditable(false);
pane.setMargin(new Insets(0,0,0,0));
try {
pane.setPage(src);
image = new BufferedImage
(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
Container c = new Container();
SwingUtilities.paintComponent
(g, pane, c, 0, 0, width, height);
g.dispose();
} catch (Exception e) {
System.out.println(e);
}
return image;
}
}
MEMBER FEEDBACK TO THIS TIP
Wouldn't it just be easier to hit the PrintScreen button or Alt+PrintScreen to bring up only the active window?
Leonard V.
Do you have comments on this tip? Let us know.
Related information from SearchDomino.com:
FAQ: Java for Lotus Notes and Domino
Reference Center: Java tips and resources
This tip was submitted to the SearchDomino.com tip library by member Michael Marcavage. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.