transparent png image loaded as opaque

i’m trying to load a transparent png image with the following code, which also converts it to a bufferedimage:


public createImage(Image img)
 {
        if(img instanceof BufferedImage)
        {
            theImage=(BufferedImage)img;    
        }
        else
        {
            MediaTracker mediaTracker=new MediaTracker(new Frame());
            mediaTracker.addImage(img,0);
            try
            {
                mediaTracker.waitForID(0);
            }
            catch(InterruptedException ie)
            {
                System.err.println(ie);
            }
            
            theImage=GraphicsEnvironment.getLocalGraphicsEnvironment().
                     getDefaultScreenDevice().getDefaultConfiguration().
                     createCompatibleImage(img.getWidth(null),
                                           img.getHeight(null),
                                           Transparency.BITMASK);
            theImage.getGraphics().drawImage(img,0,0,null);

        }
}

but when i draw the image i’ve loaded i see an opaque image, (the transparent pixels are white and opaque). if i open the image using windows picture viewer or acdc the image is displated correctly, with the transparent pixels transparet.

p.s
i’ve attached the image i can’t seem to load as transparent… maybe the problem is with it, as transparent png images i’ve downloaded from the internet work fine, while those i’ve created using GraphicsGale don’t work (though, like i’ve said before, they do work on windows picture viewer).

Try this. I made it in photoshop.
Its weird, I can save a png in Photoshop and if I make it a certain way it wont load back into PS.
But thus one loaded back ok.

For simple images like this gif is easier, especially if you are having problems.

yes, i know. but the question is what’s the difference that makes the image load correctly after you save it in photoshop…
isn’t gif a compression method that does lose data?

Try using Transparency.TRANSLUCENT, I’ve seen some weird behavior between using this and the BITMASK.

already tried it, but still, the image has a white background…

GIF has a limited color table 256 colors, and one of those colors is set to transparent. You only had about 4 colors so I think a GIF will work ok. If you were trying to convert a photo you would see some quality loss because of the limited color palette.

GIF compression is lossless.

JPEG compression is lossy, you will lose image quality when you increase the compression rate. But this gives a better image for photos, or any image with lots of color and gradients of color.

Nice book on the subject:
Designing Web Graphics
by Lynda Weinman

More than you would ever want to know on the subject :slight_smile:

I guess the difference is that you are using a program that doesnt make PNGs correctly, (or you used it wrong) and Photoshop does it right, (if you do it the right way). Try to find instructions on how to make a PNG using your program

You need to set the composite mode to something which draws the alpha over… otherwise its muliplied with whatever is there and its lost.

g.setComposite(AlphaComposite.Src);
g.drawImage…
g.dispose();

as you can see, the image i’ve created looks fine (it’s in the second post of this thread). it’s only in java that it doesn’t work.

Did you try Onyx’s suggestion?

Look back up at the post where I had a PS image, I posted yours just under it ,so it is on a gray background.
Did you intend to have the space suit area transparent?

as i said, i know that PS solves the problem. what i want to understand is why it doesn’t work using GraphicsGale. i mean, the image looks ok when you view it.

Did you intend to have the space suit area transparent?

If the answer is no then the image is not ok. When viewing it in MS viewer it puts a white color behind it but yours is white too so you cant really see the problem there.

I have no idea what Graphics Gale is so cant help there.

really? strange, i see it alright…

I have managed to create PNGs in Paint Shop Pro that wouldn’t load in Java 1.4.2. It doesn’t happen often. Last time was 6 months ago. Loading them into Microsoft Paint & then resaving them fixed the problem. The re-saved file was often slightly bigger. PNG supports quite a range of formats and optional ‘chunks’, and it wouldn’t surprise me that there are some obscure bugs in the java PNG parser, which appear with specific colour depth/size/transparency options.

Alan

I tried to point you back to my old post where I already put this on a the gray background but I’ll post it again.

You can also see this effect when you make an email message ( I tried Thunderbird) change the background color to green and drop the image in it