Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE5 Firemonkey - How to make image background transparent runtime?

I'm rewriting an old game of mine for Firemonkey, originally written in Delphi 2010. Those days it was easy making the backgrounds of the game pieces transparent using TImage.Transparent := true, but in Firemonkey I don't find it so easy. I have learned that designtime allows you to easily set the TransparentColor, and then the object background is just as transparent as you wanted it to, but how can one make this happen runtime? I'm creating a lot of dynamic TImage objects, functioning as pieces in a game, and obviously need them to have transparent backgrounds. Anyone who can help me out here? Thanks in advance.

like image 829
hastubb Avatar asked Oct 21 '25 13:10

hastubb


2 Answers

FireMonkey images use a 32-bit per pixel schema with 8 bits each for red, green and blue plus an extra 8 bits for transparency, also called an alpha channel. If the value of the alpha channel is #FF the pixel will be opaque, #00 will be completely transparent and values in between will vary the transparency accordingly.

Therefore a colour in FireMonkey (actually a TAlphaColor) can be set with an 8 digit hex number where the bits map to #AARRGGBB where AA is the transparency/alpha channel.

Thus, #FF00FF00 is opaque green and #88FF0000 is semi-transparent red. Any color value in which the first two digits are #00 with be completely transparent.

FireMonkey includes are pre-defined constant claNull for a completely transparent colour.

You can also use the TAlphaColorRec record to access individual colours of a TAlphaColor variable using it's fields, A, R, G and B. E.g.

Red := TAlphaColorRec(MyColour).R;

TAlphaColorRec(MyColour).A := #00;

like image 153
Mike Sutton Avatar answered Oct 24 '25 12:10

Mike Sutton


Yoy can change de background color (simple color) of Image and select it in the MultiResBitmap editor BEFORE load the bipmap image (bitmap with white on background):

enter image description here

Or add real transparency to the image and convert it to PNG. If you load a PNG with thansparency the editor charge it perfectly.

enter image description here

Regards.

like image 31
Germán Estévez -Neftalí- Avatar answered Oct 24 '25 11:10

Germán Estévez -Neftalí-



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!