on Firemonkey, how to draw masked bitmap on canvas?

Viewed 1771

I have a bitmap and a mask (a bitmap also). I would like to draw the bitmap on the mask (like on the picture below)

drawing bitmap on a mask

How to do this on Delphi with Firemonkey ?

2 Answers

enter image description here

Result image - star with transparent background. Use white color in your mask to show the visible part of image.
Checked in Delphi Berlin and Windows.

procedure TForm1.Button1Click(Sender: TObject);
var
  ImageRes: TResourceStream;
  Result: TBitmap;
  tmpMS : TMemoryStream;
begin
  ImageRes := TResourceStream.Create(HInstance, 'IMAGE', RT_RCDATA);
  try
    Image1.Bitmap.CreateFromStream(ImageRes);
    Image2.Bitmap.LoadFromFile('c:\temp\MaskedBitmap\Images\Mask.png');

    Result := TBitmap.Create;
    Result.CreateFromBitmapAndMask(Image1.Bitmap, Image2.Bitmap);

    // applying alpha channel to Bitmap - workaround. If you can improve write here how
    tmpMS := TMemoryStream.Create;
    Result.SaveToStream(tmpMS);
    Result.LoadFromStream(tmpMS);
    tmpMS.Free;

    Image3.Bitmap.Assign(Result);
  finally
    ImageRes.Free;
    Result.Free;
  end;
end;

Sample project

Related