CreateFromBitmapAndMask (Delphi)

From RAD Studio XE2 Code Examples
Jump to: navigation, search

Language:

Versions:

Contents

Description

This example shows how to use the CreateFromBitmapAndMask constructor. The example lets you 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 FireMonkey HD Application - Delphi, then add the next objects to the form:

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
    sourceLine, targetLine, lineCount, line: NativeInt;
    sourcePixel, targetPixel, bytes: NativeInt;
  begin
    // Extracts a centered area from the source bitmap, equal with the target bitmap
    // Finds the lines to be copied
    if Source.Height <= Target.Height then
    begin
      sourceLine := 0;
      targetLine := (Target.Height - Source.Height) div 2;
      lineCount := Source.Height;
    end
    else
    begin
      sourceLine := (Source.Height - Target.Height) div 2;
      targetLine := 0;
      lineCount := Target.Height;
    end;
    // Finds the columns to be copied
    if Source.Width <= Target.Width then
    begin
      sourcePixel := 0;
      targetPixel := (Target.Width - Source.Width) div 2;
      bytes := Source.Width * SizeOf(TAlphaColorRec);
    end
    else
    begin
      sourcePixel := (Source.Width - Target.Width) div 2;
      targetPixel := 0;
      bytes := Target.Width * SizeOf(TAlphaColorRec);
    end;
    // Copies the determinate area to the target
    for line := lineCount - 1 downto 0 do
    begin
      Move(Source.Scanline[line + sourceLine][sourcePixel],
        Target.Scanline[line + targetLine][targetPixel], bytes);
    end;
  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:

TBitmap CreateFromBitmapAndMask.PNG

Uses

See Also

Personal tools
Previous Versions