StackOverflow already mentions such a link , and the accepted answer is "casting":
Image image = ImageIO.read(new File(file)); BufferedImage buffered = (BufferedImage) image;
In my program, I try:
final float FACTOR = 4f; BufferedImage img = ImageIO.read(new File("graphic.png")); int scaleX = (int) (img.getWidth() * FACTOR); int scaleY = (int) (img.getHeight() * FACTOR); Image image = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH); BufferedImage buffered = (BufferedImage) image;
Unfortunately, I get a runtime error:
sun.awt.image.ToolkitImage cannot be attributed to java.awt.image.BufferedImage
Obviously casting is not working.
Question: what (or is) the correct way to convert Image to BufferedImage?
java casting image bufferedimage
Arek Wilk Nov 28 '12 at 12:37 2012-11-28 12:37
source share