Hi, I am trying to convert an image into grey scale. I can do this using a buffered image however the image type I have is Image. As you can see in the code below I try to cast the Image to a BufferedImage, apply the greyscale then cast is back to an Image.

MemoryImageSource src = new MemoryImageSource(grabbing.getWidth(), grabbing.getHeight(), intArr, 0, grabbing.getWidth());
        Image imageto = label.createImage(src);
        BufferedImage buff = (BufferedImage) imageto;
        Image grayImage = (Image)createGrayscalePic(buff);
        ImageIcon im = new ImageIcon(grayImage);
        frame1.getContentPane().add(label, BorderLayout.CENTER);

However I get an exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage

and the line that causes this exception is :

BufferedImage buff = (BufferedImage) imageto;

Does anyone have any ideas how I can solve this problem.

The is the method that converts a buffered image to greyscale:

public BufferedImage createGrayscalePic(BufferedImage raw) {
        BufferedImage temp = new BufferedImage(raw.getWidth(), raw.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        temp.getScaledInstance(WIDTH, WIDTH, WIDTH);
        Graphics g = temp.getGraphics();
        g.drawImage(raw, 0, 0, null);

        return temp;
This article has been dead for over six months. Start a new discussion instead.