Link to home
Start Free TrialLog in
Avatar of VSmolensky
VSmolensky

asked on

Delphi: making a BW image transparent

In my FireMonkey project, I have a TImage on a form, with a black-and-white bitmap. It's invisible, I only use it to copy its fragments for showing them in other images. The problem is to make the white color always and completely transparent. Of course, I could use TColorKeyAlphaEffect - however, it changes not the bitmap but the way it's drawn, so the copied fragments are still completely opaque. I tried to use MakeScreenshot method to save the result of TColorKeyAlphaEffect in the original bitmap, but it doesn't seem to work (though it works with other effects, like TBlurEffect). It looks like MakeScreenshot ignores the alpha channel. Any advice?

Another possible approach could be to provide the transparency at the design time. But how can I add an alpha channel to a BW 1-bit bitmap file? Is there a way to do it, for example, in Photoshop? I tried but it looks like alpha channels in Photoshop and in FireMonkey are two different things, just named the same.
SOLUTION
Avatar of Merete
Merete
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VSmolensky
VSmolensky

ASKER

Thank you, my friends, your recommendations indeed helped me create transparent files. Unfortunately, FireMonkey doesn't recognize them as transparent. Looks like I must find a way to do it by programming...
are you using an image format that supports transparency?

GIF, PNG, JPG. Which One To Use?: https://www.sitepoint.com/gif-png-jpg-which-one-to-use/
> are you using an image format that supports transparency?

Yes, I tried PNG.
It looks like I have eventually found a programming solution to my problem. But all the three suggestions were quite interesting, letting me learn something new. I thank you all!
You're welcome. And thanks to you for the update. I'm curious — what programming solution did you find?
Just changing the color, pixel by pixel:

procedure TJPForm.MakeImageTransparent(Image: TImage);
var M: TBitmapData;
    X,Y: integer;
begin
 Image.Bitmap.Map(TMapAccess.ReadWrite,M);
 for X:=0 to Image.Bitmap.Width-1 do
  for Y:=0 to Image.Bitmap.Height-1 do
   if M.GetPixel(X,Y)=claWhite
      then M.SetPixel(X,Y,TAlphaColorRec.Null);
 Image.Bitmap.Unmap(M);
 Invalidate;
end;

I just didn't know how to access pixels in FireMonkey; now I do.
I stopped using Turbo Pascal many years ago before it morphed into Delphi, so I'm not up on what you posted, but it certainly looks like a nice piece of code — thanks for sharing!