Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How can an Image be made from a GUI Component like Button or Label ?

/*

Hi guys

i found following algorithm which can make an Image from the content of a component.

what i realy want to make happen here is i have drawn an ImageButton ( without image file) and i want that image face to be saved on an image file.

i tried to use this method but image goes complete black

any clue ? */

public static BufferedImage convertComponentToImage(Component c) {

Dimension size = c.getSize();

// Create a buffered image equal to the size of the component.

BufferedImage bufferedImage = new BufferedImage(size.width, size.height,BufferedImage.TYPE_INT_RGB);

// Get the graphics context of the image

Graphics bufferedGraphics = bufferedImage.createGraphics();

// Request the component to paint itself on to the image's graphics. This method would not work if the component is not yet visualized.

c.paint(bufferedGraphics);

return bufferedImage;
}
[1027 byte] By [danushka] at [2007-11-11 8:49:55]