Java BufferedImage to Transparent makeColorTransparent(BufferedImage im, final Color color)

Here you can find the source of makeColorTransparent(BufferedImage im, final Color color)

Description

make Color Transparent

License

Open Source License

Declaration

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

public class Main {
    public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
        RGBImageFilter filter = new RGBImageFilter() {
            public int markerRGB = color.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    return 0x00FFFFFF & rgb;
                } else {
                    return rgb;
                }/* w  w  w. ja v  a  2s .c  o  m*/
            }
        };
        ImageProducer ip = new FilteredImageSource(im.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;
    }
}

Related

  1. getTransparentImage(BufferedImage i)
  2. getTransperentImage(BufferedImage origImage, BufferedImage compImg)
  3. makeColorTransparent(BufferedImage im, final Color color)
  4. makeColorTransparent(BufferedImage image, Color color)
  5. makeColorTransparent(final BufferedImage im, final Color color)
  6. makeColorTransparent(final BufferedImage im, final Color colorToMakeTransparent, final int tolerance)
  7. makeColorTransparent(Image im, final Color color)