Creating transparent pixels in PNG/Buffered Image

Posted on 2006-05-28
Last Modified: 2010-03-31
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 = ;
      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.

Question by:dpearson
    LVL 14

    Expert Comment

    read the article about Maintaining Transparency for Image Transformations
    LVL 26

    Author Comment

    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...

    LVL 14

    Expert Comment


    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
    LVL 26

    Author Comment

    Thanks for the tip.  I've posted my question there too.
    LVL 26

    Author Comment

    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 =;
    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);

    Accepted Solution

    PAQed with points refunded (250)

    Community Support Moderator

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsFront challenge 8 49
    count11 challenge 6 35
    Systems talking to each other 5 38
    Java Message handling in Service Layer 3 22
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now