Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an ImageIcon to a BufferedImage

I've been trying to convert a ImageIcon to BufferedImage... And I've had no luck.

I have a pre-existing ImageIcon that needs to be converted to a Buffered Image for the vast amount of BufferedImage operations that exist.

I have found a few ways, but all of them are hugely CPU intensive.

like image 363
Caelum Avatar asked Feb 24 '13 15:02

Caelum


People also ask

How do I change an image to BufferedImage?

BufferedImage buffer = ImageIO. read(new File(file)); to Image i.e in the format something like : Image image = ImageIO.

What is the difference between BufferedImage and image?

A BufferedImage is essentially an Image with an accessible data buffer. It is therefore more efficient to work directly with BufferedImage. A BufferedImage has a ColorModel and a Raster of image data. The ColorModel provides a color interpretation of the image's pixel data.

What does BufferedImage mean?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).

What is the use of BufferedImage?

It is used to handle and manipulate the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage objects have an upper left corner coordinate of (0, 0).


2 Answers

What's wrong with:

BufferedImage bi = new BufferedImage(
    icon.getIconWidth(),
    icon.getIconHeight(),
    BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
// paint the Icon to the BufferedImage.
icon.paintIcon(null, g, 0,0);
g.dispose();
like image 80
Werner Kvalem Vesterås Avatar answered Oct 30 '22 01:10

Werner Kvalem Vesterås


See ImageIcon, Image and BufferedImage:

ImageIcon yourImage;
Image image = yourImage.getImage();
BufferedImage buffered = (BufferedImage) image;
like image 35
Martin Thoma Avatar answered Oct 30 '22 00:10

Martin Thoma