dcsimg
Saving GIF files with transparent color
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Adolfo_Aladro
Posted On:   Thursday, August 1, 2002 12:41 AM

I am developing a servlet who creates a black and white GIF file with white as transparent color. First of all I create a BufferedImage object: BufferedImage bufferedImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_ARGB); The I get the Grpahics2D object using the getGraphics method: Graphics2D g2D = (Graphics2D)bufferedImage.createGraphics(); Then I draw a rectangle that covers the whole image. That rectangle is suposed to be white, and the white color must be the transparent one: g2D.setColor(Color.white); g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); g2D.fillR   More>>

I am developing a servlet who creates a black and white GIF file with white as transparent color.


First of all I create a BufferedImage object:


BufferedImage bufferedImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_ARGB);


The I get the Grpahics2D object using the getGraphics method:


Graphics2D g2D = (Graphics2D)bufferedImage.createGraphics();


Then I draw a rectangle that covers the whole image. That rectangle is suposed to be white, and the white color must be the transparent one:


g2D.setColor(Color.white);
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));

g2D.fillRect(0, 0, iWidth, iHeight);


Now I start drawing the black pixels of the image:


g2D.setColor(Color.black);

g2D.setComposite(AlphaComposite.SrcOver);

for(int i=0; i

  for(int j=0; j

    ...

  }

}


Finally, I use the www.acme.com package in order to encode the image as a GIF file:


FileOutputStream fos = null;

try {

  fos = new FileOutputStream("...");

  GifEncoder ge = new GifEncoder(bufferedImage.getSource(), fos);

  ge.encode();

} catch (Exception e) {

  e.printStackTrace();

} finally {

  try {

    if (fos!=null) {

      fos.flush();

      fos.close();

    }

  } catch (Exception e) {

    e.printStackTrace();

  }

}


The problem within this code is the following: when you see the generated image using Internet Explorer, or any browser, it seems to be perfect, but when you try to open the GIF file with an application such as Paint Shop Pro, you can only see a black area instead of the image.


What happens? Can anyone give me a clue?

   <<Less
About | Sitemap | Contact