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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/
GIF, PNG, JPG. Which One To Use?: https://www.sitepoint.com/gif-png-jpg-which-one-to-use/
ASKER
> are you using an image format that supports transparency?
Yes, I tried PNG.
Yes, I tried PNG.
ASKER
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?
ASKER
Just changing the color, pixel by pixel:
procedure TJPForm.MakeImageTranspare nt(Image: TImage);
var M: TBitmapData;
X,Y: integer;
begin
Image.Bitmap.Map(TMapAcces s.ReadWrit e,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,TAlphaColor Rec.Null);
Image.Bitmap.Unmap(M);
Invalidate;
end;
I just didn't know how to access pixels in FireMonkey; now I do.
procedure TJPForm.MakeImageTranspare
var M: TBitmapData;
X,Y: integer;
begin
Image.Bitmap.Map(TMapAcces
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,TAlphaColor
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!
ASKER