FMXTBitmapCanvas (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TBitmap.Canvas property. This sample draws a rectangle on an image.

To build and test this example, create a Multi-Device Application - Delphi, then add the next objects to the form:

Add the following code to the OnClick event handlers of the load button.

Code

procedure TForm1.Button2Click(Sender: TObject);
begin
   if OpenDialog1.Execute then
   begin
     Image1.Bitmap.LoadFromFile(OpenDialog1.FileName);
   end;
end;

Add the following code to the OnClick event handlers of the other button.

Code

procedure TForm1.Button1Click(Sender: TObject);
var
  MyBitmap: TBitmap;
  MyRect: TRectF;
begin
  MyBitmap := TBitmap.Create(0, 0);
  try
    if Image1.Bitmap.IsEmpty then
      // Display a message when there is no image loaded
      TDialogServiceAsync.MessageDialog(
        'There is no image to customize:',
        TMsgDlgType.mtWarning, [TMsgDlgBtn.mbOk], TMsgDlgBtn.mbOK,0)
    else
    begin
      // The rectangle to be drawn on the canvas
      MyRect.Create(50, 30, 150, 200); // A record on the stack, does not Free
      // A copy of the initial bitmap
      MyBitmap.Assign(Image1.Bitmap);
      // Draw a rectangle on the copy
      with MyBitmap.Canvas do
      begin
        BeginScene;
        Stroke.Kind := TBrushKind.Solid;
        Stroke.Color := claLime;
        StrokeThickness := 4;
        DrawRect(MyRect, 20, 20, AllCorners, 1.0);
        EndScene
      end;
      // Display the result
      Image2.Bitmap := MyBitmap;
    end;
  finally
    MyBitmap.Free;
  end;
end;

The result should look like in the following image:

TBitmap Canvas proprety.PNG

Uses

See Also