1 Reply Latest reply on Feb 7, 2013 8:53 PM by Maxideon

    GIF image loses transparency after converting to byte[]


      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);
              try {
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  ImageIO.write( bi, "GIF", baos );
                  imageInByte = baos.toByteArray();
              } 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[]
          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"?