FMXTBitmapCanvas (C++)

From RAD Studio Code Examples
Jump to: navigation, search


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:


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

void __fastcall TForm1::Button2Click(TObject *Sender) {
	if (OpenDialog1->Execute()) {

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:",
			TMsgDlgButtons() << TMsgDlgBtn::mbOK, 0);
		else {
			// A copy of the initial bitmap
			// Draw a rectangle on the copy
			MyBitmap->Canvas->Stroke->Kind = TBrushKind::Solid;
			MyBitmap->Canvas->Stroke->Color = claLime;
			MyBitmap->Canvas->StrokeThickness = 4;
			MyBitmap->Canvas->DrawRect(MyRect, 20, 20, AllCorners, 1.0);
			// Display the result
			Image2->Bitmap = MyBitmap;
	__finally {
		delete MyBitmap;


The result should look like in the following image:

TBitmap Canvas proprety.PNG


See Also