readthefuckingmanual.net

[SOLVED] java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage

Error added: 2008-02-26T19:19:01Z

2 people waiting for the answer...

2 answers found.

Answer 1945 (61.53846% helpful)

This answer is garbage. did he pull ImageRepresentation out of his ass?
'
Permalink

Answer 1221 (44.117645% helpful)

Hello, 

It works on 100%. 
You can not directly cast a hide ToolkitImage object to BufferedImage or RenderedImage. Instead You will have to create a ImageRepresentation object form ToolkitImage, and use a method to obtain a BufferedImage.

This method is called a getOpaqueRGBImage().

I hope this helps.

Best regards.
Java EE and Web Service developer and project manager.

String imageFile = \"C:/Documents and Settings/Admin/Moje dokumenty/compressed/anka.jpg\";
        BufferedImage i = ImageIO.read(new File(imageFile));
        Image imgToProcessing = (Image) i;
        Image img = (java.awt.Image) imgToProcessing.getScaledInstance(169, 385, Image.SCALE_REPLICATE);
        System.out.println(\" \\n This is img: \" + img);
 ToolkitImage toolkitImage = (ToolkitImage) img;
        System.out.println(\" toolkitImage: \" + toolkitImage);
        ImageRepresentation ir = toolkitImage.getImageRep();
        System.out.println(\" ir: \" + ir.getOpaqueRGBImage());
        BufferedImage bb = ir.getOpaqueRGBImage();
Permalink

Add an answer/solution

If you know the answer, please add your own solution below.
If you don't know, but find out later, please come back and share your answer - there will be other people struggling with this too.


Please enter 61948 here

If you want to be notified via email when this is solved, enter your email address here: