This discussion is archived
1 Reply Latest reply: Feb 7, 2013 12:53 PM by Maxideon RSS

GIF image loses transparency after converting to byte[]

433331 Newbie
Currently Being Moderated
Hi

I have a transparent background gif file which I am reading into an Image ....

I am writing this Image to a mysql BLOB column after converting it to a byte[] and am losing the transparency in the process ...
Any help would be greatly appreciated. Thanks
public byte[] getSignImageBytes() {

        byte[] imageInByte = null;

        int type = BufferedImage.TRANSLUCENT;

        BufferedImage bi = new BufferedImage(gifImage.getWidth(null), gifImage.getHeight(null), type);
        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(gifImage, 0, 0, null);
        g2d.dispose();
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            ImageIO.write( bi, "GIF", baos );
            baos.flush();
            imageInByte = baos.toByteArray();
            baos.close();
        } catch (IOException e) {}
        return imageInByte;

    }
Edited by: nla on Feb 7, 2013 4:49 PM
  • 1. Re: GIF image loses transparency after converting to byte[]
    Maxideon Explorer
    Currently Being Moderated
    The gif image format saves palette based images (up to 256 colors), that is a BufferedImage of TYPE_BYTE_INDEXED. If you don't pass the writer such an image, it will color quantize the image to make it one. You're likely loosing the alpha color in that process.

    What happens if you just write "gifImage" directly, and skip the intermediate image? What is "gifImage"?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points