Java 2D - Need urgent help ..save BMP image

I have a BMP image .I m extracting itspixels (using PixelGrabber)and formatting those values as per requirements.

Now,I have tocreate an image using this pixels array again.

Image img;

img = createImage(new MemoryImageSource(iw,ih,pixels,0,iw)) ;

This is creating an image "img" of type Image.

Now,I have tosave this image as BMP.

for this I am using

ImageIO.write(img,"BMP",new File("a.bmp"));

The problem is this write method require an image of typeBuffered Image.

I am not able totypecast an object of type Image into BufferedImage.

BufferedImage is subclass of Image class..

I just hope i m making sense..

Please help me..

Is there any other way too to save an BMP image created by createImage()

[1123 byte] By [richa_sharmaa] at [2007-11-26 23:29:18]
# 1

Hi richa,

here is what I would do to convert a MemoryImageSource into a BufferedImage:

BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(),BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = copy.createGraphics();

g2d.drawImage(img, 0, 0, null);

g2d.dispose();

This code basically creates a new BufferedImage of the same size as your MemoryImageSource image (img) and fill it with the image.

Cheers.

Vince.

blendinga at 2007-7-10 14:39:40 > top of Java-index,Security,Cryptography...
# 2

Thnx a lot for help I used a method u told me ...

here is the code i hve:

import java.io.*;

import java.awt.*;

import javax.swing.*;

import javax.imageio.*;

import java.awt.image.*;

import java.awt.image.PixelGrabber;

import javax.imageio.ImageIO;

public class RecreateImage2

{

public static void main(String args[])

{

ImageFrame frame = new ImageFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class ImageFrame extends JFrame

{

public ImageFrame()

{

Toolkit kit=Toolkit.getDefaultToolkit();

Dimension screenSize=kit.getScreenSize();

int screenHeight=screenSize.height;

int screenWidth=screenSize.width;

setSize(screenWidth/2,screenHeight/2);

setLocation(screenWidth/4,screenHeight/4);

setTitle("Centered Frame");

ImagePanel1 panel=new ImagePanel1();

add(panel);

}

}

class ImagePanel1 extends JPanel

{

Image img;

public ImagePanel1()

{

try

{

Image image;

Dimension d;

int iw,ih;

image = ImageIO.read(new File("bug_apple.bmp"));

iw = image.getWidth(null);

ih = image.getHeight(null);

int pixels[] = new int[ih*iw];

PixelGrabber pg = newPixelGrabber(image,0,0,iw,ih,pixels,0,iw);

pg.grabPixels();

img = createImage(new MemoryImageSource(iw,ih,pixels,0,iw));

BufferedImage bi = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB)

;

Graphics2D g2d = bi.createGraphics();

g2d.drawImage(img, 0, 0, null);

g2d.dispose();

img=bi;

ImageIO.write(img,"BMP",new File("a.bmp")) ;

}

catch(InterruptedException e)

{}

catch(IOException e)

{

e.printStackTrace();

}

}

}

Its giving error ...saying

cannot find method

write(java.awt.Image,java.lang.String,java.io.File)

But If i use Method

ImageIO.write((BufferedImage)img,"BMP",new File("a.bmp")) ;

It saves the Image as "a.bmp" but preview of image is not available...

Please help me..I really dono whr the proplem is..

richa_sharma

richa_sharmaa at 2007-7-10 14:39:40 > top of Java-index,Security,Cryptography...
# 3

I'd suggest to check return value of the ImageIO.write() method. It indicates whether writing operation was succeed. This writing failure seems to be caused by usage of unsuitable buffered image type. Unfortunately, ImageIO does not allow to encode image with alpha channel as bmp. You workaround this limitation by creating buffered image without alpha channel (TYPE_INT_RGB for example) or (if alpha channel is important) by encoding to other format like png.

Andrew.Brygina at 2007-7-10 14:39:40 > top of Java-index,Security,Cryptography...