Creating a transparent image

Hi.

I require to use a transparent image in my program. I have downloaded photoshop and I have made, what I think is a transparent image, but when I load it with imageicon and then display it, it doesn’t work.

For example, in photoshop you can select what parts you want to be transparent and then use a wizard to make that transparent. So I then take this file from the wizard and try to load it into my program and I just got a black box where I selected my area to be transparent and then, there is the rest of the white background.

So basically, I have 2 questions:

1.) Can image icon be used with transparent images?
2.) If anyone knows how to make a transparent image in photoshop can you tell me how to do it? I am not entirely sure I am doing it right because it isn’t working. I was playing around with it and I can’t really make them transparent in webpages either, to come think of it. I was going to post on the PS forums but I suck and I can’t find the “Add post” button they claim is there haha.

So basically, I’m TOTALLY clueless on this. I’ve been trying to get this damn thing to work for about 3 hours haha!

Thanks.

I know the feeling of trying and trying and still being stoke :slight_smile:

new ImageIcon(…).getImage should be able to return a transparent image. However, if you post some code it will be easier to guide you.

I am not familiar with Photoshop but try performing a google. A fast search gave the following result that might be usefull: http://www.mediacollege.com/graphics/photoshop/transparent/background.html

Good luck!

I’m using photoshop 9 cs. I don’t know about wizzard you’re talking about, but you can create transparent image with new image (file - new) and then selecting transparent in background selection. Transparent areas are shown as white-gray chessboard. You can then paste any image you want and delete stuff from that image to get to transparent area. For example if you paste some image that isn’t transparent but has white background you use magic wand on white backround to select it and then delete the selection.
Once you create transparant image you must save it in format that supports transparancy, like gif or png, jpg dosen’t support it. Note that gif supports transparancy but not translucency, for that you need to use png. Note also that pngs aren’t hardware accelareted in windows :slight_smile: … sucks huh? :wink:

A gross simplification.

What you mean is - a png that uses full alpha translucency will not be accelerated in windows unless the opengl pipeline, or special d3d flags are enabled.

You’re right, thank you.

Thanks for the link, that looks TOTALLY different then what PS told me to do. They have this thing called export to transparent image wizard, so I did that but it never worked.

btw, I have the ogl pipeline enabled, so I am getting hardware accelration. I am going to try out what you guys posted and see if I can get it to work now.

Thanks!

Edit: Well I tried this out and it seems I was using a bufferedimage and not an imageicon. I managed to get the transparency to work in a website, but when I use the image in the program, it does not work.

Here is the function that I am using. It is from a tutorial from somewhere:


 public static BufferedImage getBufferedImage(String imageFile,
                                               Component c) {
    Image image = c.getToolkit().getImage(imageFile);
    waitForImage(image, c);
    BufferedImage bufferedImage =
      new BufferedImage(image.getWidth(c), image.getHeight(c),
                        BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    g2d.drawImage(image, 0, 0, c);
    return(bufferedImage);
  }

I suspect that it is the TYPE_INT_RGB part and I have to replace it with someone else.

Thanks,

YEP! I was right, I had to include alpha in and it works great.

THanks for your help!

Here’s an easier way:



import javax.imageio.*;
import java.io.*;
...

public static BufferedImage getBufferedImage(String imageFile) {
    BufferedImage bImage = null;
    try {
        bImage = ImageIO.read(new File("myImage.png"));
    } catch (IOException e) {
        //.. 
    }
    return bImage;
}