• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

Creating transparent pixels in PNG/Buffered Image

I am attempting to write a small utility to scale 8-bit PNG files.  However, when I write out the scaled image the transparent field is getting mapped to a color rather than transparency.

The basic method is very simple:
      BufferedImage bi = ImageIO.read(srcFile) ;
      Image scaled = bi.getScaledInstance(targetWidth, targetHeight, Image.SCALE_FAST) ;
      ColorModel cm = bi.getColorModel() ;
      BufferedImage out = toBufferedImage1(bi, scaled, cm) ;
      ImageIO.write(out, "png", destFile);

the tricky part is converting the scaled image to a buffered image in order to write out the PNG.

I'm doing that like this:
        // Option 1 -- Using the screen as basis for gc
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        // Option 2 - using  the original image as basis for gc
        Graphics2D originalG = original.createGraphics() ;
          GraphicsConfiguration origDC = originalG.getDeviceConfiguration() ;

        BufferedImage bimage = origDC.createCompatibleImage(image.getWidth(null), image.getHeight(null), Transparency.BITMASK);

and then copy the scaled image into the new buffered image like this:
        Graphics g = bimage.createGraphics();
        g.drawImage(image, 0, 0, null);

If I use option 1 (based on the screen) I get a 24-bit PNG that has correct transparency.
If I use option 2 (based on the original image) I get an 8-bit PNG that is correct except all transparent fields are filled with an arbitrary, incorrect color.

What I want is an 8-bit PNG with correct transparency.

So, can anyone tell me how to:
     a) Create an 8-bit PNG in another way that preserves transparency?
or  b) Tell me how to make a buffered 8-bit image fully transparent (before I paint over with the image data)?  I've tried various calls to setRGB and calling writableRaster w/o success.
or  c) Give me a different way to scale an 8-bit PNG?

Thanks in advance for any suggestions.

  • 3
  • 2
1 Solution
read the article about Maintaining Transparency for Image Transformations
dpearsonAuthor Commented:
Thanks for the link.

However, it explains how to take an Image and map it to another Image while preserving transparency.

That's good, but still leaves open the question of how to get Image back out to disk as a PNG which is where my code is failing.
The original image in my case already has transparency but when it's drawn over the buffered image the result is incorrect (presumably because the buffered image doesn't start as a completely transparent image when it's an 8-bit GC).

So I'm still on the hunt for help with:
- Either how to create the buffered image in a way that preserves transparency
- or how else to write out an image as a PNG without creating a buffered image

It seems like there should be an easy way to do this but I haven't found it...


Im a bit ignorant in java's image processing api
if you post your Q in the proper topic area of the below link you may get a better answer sooner
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

dpearsonAuthor Commented:
Thanks for the tip.  I've posted my question there too.
dpearsonAuthor Commented:
Just in case anyone searches for this answer in the future here's the code that works.
The key is g2d.setComposite(AlphaComposite.Src);

BufferedImage in = ImageIO.read(srcFile);
GraphicsConfiguration gc =in.createGraphics().getDeviceConfiguration();
BufferedImage out = gc.createCompatibleImage(targetWidth, targetHeight, BITMASK);
Graphics2D g2d = out.createGraphics();
g2d.drawImage(in, 0, 0, targetWidth, targetHeight, null);
ImageIO.write(out, "png", destFile);
PAQed with points refunded (250)

Community Support Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now