How to make part of an image transparent?

  

Under delphi (Firmonkey), under android if that matters, I need to make a specific part of a bitmap transparent ie the round corner of the bitmap like in the picture below :

The bitmap is generated via the Control.MakeScreenShot.

I tried to replace all the top left red pixel by transparent pixel like below:

if fScreenShot.Map(TMapAccess.ReadWrite, M) then
try

//make the top left corner transparent
for x := 0 to fScreenShot.Width – 1 do begin
aContinue := False;
for y := 0 to fScreenShot.Height – 1 do begin
if M.GetPixel(X,Y) = _FrameBGColor then begin
aContinue := True;
M.SetPixel(X,Y,TalphaColorRec.Null);
end
else break;
end;
if not aContinue then break;
end;

finally
fScreenShot.Unmap(M);
end;

but the instruction fScreenShot.Map(TMapAccess.ReadWrite, M) is terribly slow (around 200 ms) so I can’t do like this 🙁 is there any other way I can go to replace this red color by a transparent color?

NOTE: right now (I focus only on android/ios, so on OpenGL) the only way I found to clear a region in a canvas is to do

Canvas.SaveState;
Canvas.IntersectClipRect(TRectF.create());
Canvas.Clear(TalphaColorRec.Null);
canvas.RestoreState

however this work only for rectangular area and can’t be applied to my problem 🙁

NOTE 2 I’m close to find a workable solution using blending := False :

Canvas.blending := False;
Canvas.Fill.Color := $00000000;
Canvas.Fill.Kind := TbrushKind.Solid;
canvas.FillRect(TRectF.create(100,100,200,200), //const ARect: TRectF;
0, 0, //const XRadius, YRadius: Single;
[], //const ACorners: TCorners;
1, // const AOpacity: Single;
TCornerType.round);
Canvas.blending := true;

This code actually clear the rectangle. Look good and it’s what I want to do with the corner. So I try to replace FillRect by fillpath and I’m so unlucky it’s not work with fillpath 🙁 any idea? it’s work with fillEllipse, FillRect, etc, but just with fillpath it’s not work 🙁

Comments are closed.