delphicanvasbitmapfiremonkeygraphic

FMX: Fill whole bitmap with a background color


I want to create a TImage component and fill the image with a background color. However my code is a bit longer than I have expected.

  1. I have to set the width and height of bitmap.
  2. I have to calculate the rectangle of the whole bitmap canvas.

If I remember correctly, in old Delphi versions, I can use FloodFill to fill the whole image with particular color. So I think I have definitively missed something.

Can someone figure out how to fill background color with simpler code?

Image := TImage.Create(nil);
Image.Position.X := 100;
Image.Position.Y := 100;
Image.Width := 500;
Image.Height := 500;
Image.Bitmap.Width := Trunc(Image.Width);
Image.Bitmap.Height := Trunc(Image.Height);
with Image.Bitmap.Canvas do
begin
  BeginScene;
  try
    Fill.Color := TAlphaColors.Black;
    FillRect(RectF(0, 0, Image.Bitmap.Width, Image.Bitmap.Height), 0, 0, [], 1.0);
  finally
    EndScene;
  end;
end;

Solution

  • There is no FloodFill in FMX. But you can use Clear(TAlphaColors.Black); in order to fill the entire bitmap with a color.