FMXTBitmapCanvas (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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

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

Code

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

void __fastcall TForm1::Button2Click(TObject *Sender) {
	if (OpenDialog1->Execute()) {
		Image1->Bitmap->LoadFromFile(OpenDialog1->FileName);
	}
}

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


void __fastcall TForm1::Button1Click(TObject *Sender) {
	TBitmap *MyBitmap = new TBitmap(0, 0);
	// The rectangle to be drawn on the canvas
	TRectF MyRect = TRectF(50, 30, 150, 200);
	// A record on the stack, does not Free
	try {
		if (Image1->Bitmap->IsEmpty()) {
			// Display a message when there is no image loaded
			MessageDlg("There is no image to customize:",
				TMsgDlgType::mtWarning,
			TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
		}
		else {
			// A copy of the initial bitmap
			MyBitmap->Assign(Image1->Bitmap);
			// Draw a rectangle on the copy
			MyBitmap->Canvas->BeginScene();
			MyBitmap->Canvas->Stroke->Kind = TBrushKind::Solid;
			MyBitmap->Canvas->Stroke->Color = claLime;
			MyBitmap->Canvas->StrokeThickness = 4;
			MyBitmap->Canvas->DrawRect(MyRect, 20, 20, AllCorners, 1.0);
			MyBitmap->Canvas->EndScene();
			// Display the result
			Image2->Bitmap = MyBitmap;
		}
	}
	__finally {
		delete MyBitmap;

	}
}

The result should look like in the following image:

TBitmap Canvas proprety.PNG

Uses

See Also