public static BufferedImage makeColorTransparent ( BufferedImage bufferedImageToProcess , final Color colorToMakeTransparent ) { ImageFilter filter = new RGBImageFilter ( ) { public int markerRGB = colorToMakeTransparent . getRGB ( ) | 0xFF000000 ; public final int filterRGB ( int x , int y , int rgb ) { if ( ( rgb | 0xFF000000 ) == markerRGB ) { // Mark the alpha bits as zero - transparent return 0x00FFFFFF & rgb ; } else { return rgb ; } } } ; ImageProducer ip = new FilteredImageSource ( bufferedImageToProcess . getSource ( ) , filter ) ; Image image = Toolkit . getDefaultToolkit ( ) . createImage ( ip ) ; BufferedImage bufferedImage = new BufferedImage ( image . getWidth ( null ) , image . getHeight ( null ) , BufferedImage . TYPE_INT_ARGB ) ; Graphics2D g2 = bufferedImage . createGraphics ( ) ; g2 . drawImage ( image , 0 , 0 , null ) ; g2 . dispose ( ) ; return bufferedImage ; }
Make a color of the image transparent .