converting webpage to image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Himzi
    New Member
    • Jul 2008
    • 9

    converting webpage to image

    Hi,
    I am trying to convert an HTML to an image but the image that gets generated doesnt contain the images in that webpage.
    my code is :-
    Code:
    Import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    public class MyConverter {
        private static volatile boolean loaded;
    
        public static void main(String[] args) throws IOException {
            loaded = false;
    
           URL url = new URL("http://www.google.com");
            JEditorPane editorPane = new JEditorPane();
            editorPane.addPropertyChangeListener(new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equals("page")) {
                        loaded = true;
                    }
            });
    
            editorPane.setPage(url);
            while (!loaded) {
                Thread.yield();
            }
    
            File file = new File("test.png");
    
            componentToImage(editorPane, file);
        }
    
        public static void componentToImage(Component comp, File file) throws IOException {
            Dimension prefSize = comp.getPreferredSize();
            BufferedImage img = new BufferedImage(prefSize.width, comp.getPreferredSize().height,
                                                  BufferedImage.TYPE_INT_ARGB);
            Graphics graphics = img.getGraphics();
            comp.setSize(prefSize);
            comp.paint(graphics);
            ImageIO.write(img, "png", file);
        }
    }

    Please can someone tell me what I need to do to make sure that the images on the webpage also show up in the final png being created?


    Thanks,
    Himzi
Working...