How do I write JPG or GIF files

Hi,Is there anyway I can write the Image object into GIF or JPG file appropriately?Thanks!Regards,Thomas.
[147 byte] By [thomascheah] at [2007-9-26 8:11:12]
# 1

I replied to a similar question in the Java programming forum. Here is the URL:

http://forum.java.sun.com/thread.jsp?forum=31&thread=174657

Here are steps to convert Image to BufferedImage,

1. Construct a BufferedImage object

2. Get the graphics context

3. Draw the Image object over this graphics context.

public static BufferedImage convertToBufferedImage(Image img,

int wid, int ht, int type){

BufferedImage bi = new BufferedImage(wid, ht, type);

Graphics2D g2d = bi.createGraphics();

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

return bi;

}

The type parameter represents the type of the BufferedImage (such as TYPE_INT_RGB). See the Javadocs for the BufferedImage class.

To write to a GIF file, you may have to use a commercial package.

larryhr at 2007-7-1 18:36:51 > top of Java-index,Security,Cryptography...
# 2

Things NOT to forget about when storing your Java Image as GIF and/or JPEG:

- Java Images can have LOTS of color depth, GIF files are limited to 256. Take care to reduce your colors. The acme component does NOT perform color reduction, you have to figure that one out on your own.

- If it has one, preserve the Java Image's transparency using a color not within the final image's colors.

- The older JPEG format does NOT support transparency.

noitall at 2007-7-1 18:36:51 > top of Java-index,Security,Cryptography...