0

Looking for the least painful way to grab a Bitmap and translate every pixel of a specific color to transparent.

In particular, I have set all the pixels I want to be transparent to 0xFF00FF, but I am not seeing any native method that do this.

Do I have to actually loop through all the pixels comparing values? And if so, can I change them in place or do I need to make a new bitmap array and create a new bitmap?

Charles
  • 50,943
  • 13
  • 104
  • 142
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • 1
    You know that the bmp format doesn't support transparent pixels, don't you? – Thomas Jan 24 '12 at 22:02
  • I'm not sure what you mean... I can set the individual pixels to Color.TRANSPARENT, and I have no problem loading PNGs with alpha transparency into Bitmaps and they display correctly. So... offhand your comment makes no sense. – Yevgeny Simkin Jan 24 '12 at 22:41
  • check this post http://stackoverflow.com/questions/4251027/faster-way-to-set-a-png-bitmap-color-instead-of-pixel-by-pixel – Sergey Benner Jan 25 '12 at 01:31
  • To clarify: bitmap is a somewhat ambigous term, but you used BMP in your question. Those are not PNG files and don't support transparency. That said you can load a BMP into memory and set color keyed pixels (pixels of a defined color) transparent, but you can't write that transparency back to a BMP file. - From your question it is not quite clear whether you want to write the information back to a file or just render that image to the screen. – Thomas Jan 25 '12 at 07:35

1 Answers1

0

Bitmaps don't have alpha channels like PNG/GIF. It's up to the application to use a transparency mask.

theannouncer
  • 1,148
  • 16
  • 28
  • Ok, maybe I'm being unclear... I have Bitmap data in an array. I can set any individual pixel to 0x0 (Color.TRANSPARENT) and as long as the Bitmapl.hasAlpha() == true, it should render that pixel as transparent. You're right that if I save it out to a JPG or a BMP it won't be transparent, but a Bitmap in Java can absolutely have transparent pixels, I'm just asking if there's an easy way to convert pixels of value x (0xffff00ff for example) to 0x0. – Yevgeny Simkin Jan 24 '12 at 23:57
  • 1
    You can always iterate through the array and test and convert each pixel. Take a look at BufferedImage http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html I believe it can find pixels of a certain rgb and convert them. – theannouncer Jan 25 '12 at 00:12