CreateFromBitmapAndMask (Delphi)
Contents
Description
This example shows how to use the CreateFromBitmapAndMask constructor. The example allows you to load two images (an initial bitmap and a mask), resizes them to the same size, and then creates a new image by applying the mask over the initial image.
To build and test this example, create a Multi-Device Application - Delphi, then add the next objects to the form:
- A TImage to display the initial TBitmap and a TLabel for it.
- A TImage to display the mask and a TLabel for it.
- Two TImage to display the resized mask and the initial bitmap, and a TLabel for them.
- A TImage to display the created bitmap and a TLabel for it.
- A TOpenDialog.
- A TButton to load the initial bitmap image.
- A TButton to load the mask.
- A TButton to create and display the result.
Code
Add the following code to the OnClick event handlers of the button that loads the initial bitmap.
procedure TForm1.Button1Click(Sender: TObject);
begin
// Loads the source bitmap
if OpenDialog1.Execute then
begin
Image1.Bitmap.LoadFromFile(OpenDialog1.FileName);
end;
end;
Add the following code to the OnClick event handlers of the button that loads the mask.
procedure TForm1.Button2Click(Sender: TObject);
begin
// Loads the mask bitmap
if OpenDialog1.Execute then
begin
Image2.Bitmap.LoadFromFile(OpenDialog1.FileName);
end;
end;
Add the following code to the OnClick event handlers of the button to create and display the result. The nested CenterCopy
and CreateSameSizeBitmaps
procedures are used to resize the initial bitmap and the mask to the same size. One uses the Min
function, which is part of the Math
unit.
procedure TForm1.Button3Click(Sender: TObject);
// The source bitmap and the mask will have the same size
procedure CenterCopy(Source, Target: TBitmap);
var
X,Y:integer;
TopLeft: TPoint;
Rect:TRect;
begin
X:=Source.Width div 2;
Y:=Source.Height div 2;
//Define the top left corner of the rectangle to be copied
TopLeft:=Point(X-Target.Width div 2, Y-Target.Height div 2);
//Define the rectangle to be copied
Rect:=TRect.Create(TopLeft,Target.Width,Target.Height);
Target.CopyFromBitmap(Source,Rect,0,0);
end;
procedure CreateSameSizeBitmaps(Source1, Source2: TBitmap;
var Target1, Target2: TBitmap);
var
minX, minY: Integer;
begin
// Resizes the initial bitmap and the mask to the same sizes
// Finds the size of the result bitmap
minX := Min(Source1.Width, Source2.Width);
minY := Min(Source1.Height, Source2.Height);
Target1 := TBitmap.Create(minX, minY);
Target2 := TBitmap.Create(minX, minY);
// Resizes to the initial bitmap
CenterCopy(Source1, Target1);
// Resizes to the mask
CenterCopy(Source2, Target2);
end;
var
Bitmap1, Bitmap2, Bitmap3: TBitmap;
begin
// Resizes the source and the mask
CreateSameSizeBitmaps(Image1.Bitmap, Image2.Bitmap, Bitmap1, Bitmap2);
try
// Creates the final bitmap by applying the mask over the source
Bitmap3 := TBitmap.CreateFromBitmapAndMask(Bitmap1, Bitmap2);
try
// Displays the result
Image3.Bitmap := Bitmap3;
// Image3.Bitmap.UpdateHandles;
finally
// Frees the result bitmap
Bitmap3.Free;
end;
// Displays the resized images (source and mask)
Image4.Bitmap := Bitmap1;
Image5.Bitmap := Bitmap2;
finally
// Frees the resized source and mask bitmaps
Bitmap2.Free;
Bitmap1.Free;
end;
end;
The following image displays a result of the example:
Uses
See Also
- FMX.Objects.Objects.TImage ( fr | de | ja )
- FMX.Dialogs.TOpenDialog( fr | de | ja )