Worst way to take a screenshot of a website using HTMLEditorKIT
- javax.swing.text.html.HTMLEditorKit
- javax.swing.text.html.HTMLDocument
- javax.swing.JEditorPane
- javax.imageio.ImageIO
f.getContentPane().paint(bi.getGraphics());
ImageIO.write(bi, "png", new File(fs));
Working example of a program
ImageIO.write(bi, "png", new File(fs));
URL u = new URL("http://lenta.ru");
String fs = "lentaru.png";
JFrame f = new JFrame();
JEditorPane jep = new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit();
Container p = f.getContentPane();
jep.setEditorKit(kit);
HTMLDocument doc = (HTMLDocument)jep.getDocument();
doc.putProperty("IgnoreCharsetDirective", true);
kit.read(u.openStream(), doc, 0);
p.add(jep);
f.pack();
System.out.println("Downloaded");
BufferedImage bi = new BufferedImage(
p.getWidth(),
p.getHeight(),
BufferedImage.SCALE_DEFAULT
);
f.getContentPane().paint(bi.getGraphics());
ImageIO.write(bi, "png", new File(fs));
System.out.println(String.format(
"Saved %sx%s",
p.getWidth(),
p.getHeight()
));
System.exit(0);
June 3rd, 2011