Tuesday, March 31, 2009

Image to BufferedImage conversion

For some reason, this seems to be a largely unsolved problem on the internet. Here's the short:

1) Image im = whatever...
2) BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeight(null), type);
3) Graphics g = bi.getGraphics();
4) g.drawImage(im, 0, 0, null);

There are plenty of optional details, of course, which check for transparent pixels, create a BufferedImage that is compliant with the target platform's graphics options, etc. These steps provide you with the critical path, and leave the details to exploratory choice.

No comments: