Rendering Images Transparently

The Bitmap object supports the transparent rendering of one or more colors in an image.

If you need to render a single color transparently, use the setTransparentColor and setTransparent methods. The setTransparentColor method takes a Color object parameter that specifies the bitmap color to be rendered transparently. The setTransparent method takes a boolean value that specifies whether the designated transparent color should be rendered or not.

For example, the following code fragment designates black as the transparent color. When the Graphics object’s drawImage method is called to draw the image, the black pixels in the image are not rendered:

   protected void onPaint(PaintEvent e)
   {

      Bitmap bmp = new Bitmap(“c:\\MyImage.bmp”);
      Bmp.setTransparentColor(Color.BLACK);
      Bmp.setTransparent(false);
      e.graphics.drawImage(bmp, new Point(0,0));
      
   }

You can also achieve transparency by creating a Bitmap object based on two bitmasks, one of which is color and the other monochrome. In the color mask, each part of the bitmap to be drawn transparently should be black. In the monochrome mask, each part to be drawn transparently should be white.