In C++ Builder, it is easy to draw images on Bitmaps by using Canvas property and methods to draw, Canvas can be used on both VCL and FMX applications. Canvas provides an abstract drawing space for objects that must render their own images. TCanvas provides properties, events, and methods that assist in creating an image by:
- Specifying the type of brush, stroke, and font to use.
- Drawing and filling a variety of shapes and lines.
- Writing text.
- Rendering graphic images.
- Enabling a response to changes in the current image.
In C++ Builder FireMonkey projects, Canvas requires BeginScene() and EndScene() Methods to edit that memory area faster. We can use Canvas property of any component. Here below all samples are about Canvas of Bitmaps. Canvas of Bitmaps can be used to draw inside and all drawings should be done between these methods as below;
General usage of Canvas of a Bitmap
Using Bitmap allows you to modify canvas while you can able to show this as a Image Bitmap, you can also Load or Save this bitmap easily. Please read this article about Bitmap Operations in C++ Builder (FireMonkey).
1 2 3 4 5 6 7 8 9 10 11 12 |
TBitmap *bmp= New TBitmap(); bmp->SetSize(1920,1080) bmp->Canvas->BeginScene(); // do all drawings here bmp->Canvas->EndScene(); Image1->Bitmap->Assign(bmp); //copy bmp to Image1->Bitmap, you can directly draw to this bitmap too bmp->Free(); // delete bitmap from the memory |
Colors, Pen and Paper Color in Canvas
1 2 3 4 5 |
bmp->Canvas->Fill->Color=0xFFFFFFFF; //same as claWhite bmp->Canvas->Stroke->Color=claBlack; bmp->Canvas->Fill->Kind=TBrushKind::bkSolid; |
Drawing Lines into Canvas
1 2 3 |
bmp->Canvas->DrawLine(TPoint(50,50),TPoint(150,150), 1.0); |
Getting and Drawing Pixels into Canvas
1 2 3 4 |
T=bitmapData.GetPixel(50,100); bitmapData2.SetPixel(x,y, claRed); //set pixel to red , or u can directly use 0xFFFF0000 |
Drawing Rectangles into Canvas
1 2 3 4 |
bmp->Canvas->FillRect( TRect(0,0,320,200), 0, 0, AllCorners, 1.0); // filled bmp->Canvas->DrawRect( TRect(0,0,320,200), 0, 0, AllCorners, 1.0); // frame |
Drawing Circles and Ellipses into Canvas
1 2 3 4 |
bmp->Canvas->FillEllipse( TRect(50,50,100,100), 1.0); // filled bmp->Canvas->DrawEllipse( TRect(50,50,100,100), 1.0); // frame |
Drawing Texts in to Canvas
1 2 3 4 5 6 7 8 9 |
bmp->Canvas->FillText( TRectF( 100, 100, 200, 120), // Text area to draw L"Hello", // Unicode Text false, // Word Wrap 1.0, // Opacity 0-1.0 TFillTextFlags() << TFillTextFlag::ftRightToLeft, // Flags TTextAlign::taCenter, // Horizantal Align (taLeading, taCenter, taTrailing) TTextAlign::taCenter); // Vertical Align (taLeading, taCenter, taTrailing) |
Drawing Bitmap into Canvas of another Bitmap
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TBitmap *bmp= New TBitmap(); bmp->SetSize(1920,1080) bmp->Canvas->BeginScene(); bmp->Canvas->DrawBitmap(bmp2, TRect(0,0,bmp2->Width,bmp2->Height), // TRect(0,0,bmp2->Width,bmp2->Height), 1.0); bmp->Canvas->EndScene(); Image1->Bitmap->Assign(bmp); bmp->Free(); bmp2->Free(); |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition