Bitmap is a standard for reading and writing pixels of images of applications on all platforms (Windows, MacOS, iOS, Android, Linux, …). A Bitmap includes bitmap information and full uncompressed image in pixels. Each pixel of this uncompressed image consists 4 color members called ARGB (Alpha Red Green Blue) and it can be set or shown as 0xAABBCCDD in hexadecimal format.
A TBitmap is a powerful graphic object used to create, manipulate and store images in memory and stored as a BMP image file (or in compressed types like JPG, PNG,…) on a disk. At the time of this writing C++Builder does not support Android64 or macOS64. Using Bitmaps and doing drawings, effects and analyzing pixels are easy by using C++ Builder, Delphi programming languages. TBitmap contains an internal image of the bitmap graphic and automatically manages realization of the palette when drawn. It also has an internal Canvas, that allows you to draw pixels, lines, etc. Manipulating and copying image is very easy by using TBitmap Classes. All methods, properties and events of TBitmap can be found on this TBitmap wiki and It is possible to use multi-resolution bitmaps
Bitmaps can be easily used by TImage component in C++ Builder which has Bitmap property and it is easy to display a graphical image or graphical image files (BMP, PNG, JPG,… ) on a control. On C++Forms, Just drag Image component from the Palette (right side ) and double clikc MultiRessBitmap property from the Object Inspector (left side). Click this icon to import an image.
Moreover, you can do a lot of operations easily and faster on these images in your C++ codes. Loading, saving, drawing, copying, reading pixels etc.
Now lets see some of bitmap operation examples in C++ as listed below;
Creating a New Bitmap and Deleting Bitmap from Memory
1 2 3 4 5 6 7 |
TBitmap *bmp; bmp=new TBitmap(); ... // when done bitmap should be removed from the memory bmp->Free(); |
Setting Size of Bitmap, Resizing Bitmaps
1 2 3 4 5 |
TBitmap *bmp=new TBitmap(400,200); ... bmp->Free(); |
1 2 3 4 5 6 7 |
TBitmap *bmp; bmp=new TBitmap(); bmp->SetSize(1920,1080); ... bmp->Free(); |
In these examples you cant see these bitmaps, because it is totaly in 0x00000000 form, which means it is fully transparent. You need to clear this blank bitmap in color if you want to see.
Clearing Bitmap in Colors
1 2 3 4 5 6 7 8 9 |
TBitmap *bmp=new TBitmap(400,200); bmp->Clear(0xFF000000); // ARGB form, FF part is for alpha, lower this to do transparent bmp->Clear(claWhite); ... bmp->Free(); |
Copying a Bitmap to Another Bitmap
1 2 3 4 5 6 7 8 9 |
TBitmap *bmp1, *bmp2; bmp1=new TBitmap(); bmp2=new TBitmap(); bmp2->Assign(bmp1); ... bmp1->Free(); bmp2->Free(); |
Loading Bitmap from a File and Saving Bitmap to a File
1 2 3 4 5 6 7 |
TBitmap *bmp=new TBitmap(); bmp->LoadFromFile("D:\\test.jpg"); Image1->bmp->Assign(bmp); bmp->SaveToFile("D:\\test.bmp"); bmp->Free(); |
Drawing A Bitmap on to Another Bitmap (Including Transparency and Resizing Features)
In C++ Builder with FireMonkey C++ projects (Windows, Mac-OS, iOS, Android) , you can draw a bitmap on to another bitmap easily and faster by using DrawBitmap command. This works really fast because of locking bitmap data area by BeginScene() and EndScene() commands and you directly manuplate over the memory.
1 2 3 4 5 6 7 |
TBitmap *bmp=new TBitmap(200,200); TBitmap *bmp2=new TBitmap(200,200); bmp->Canvas->BeginScene(); bmp->Canvas->DrawBitmap( bmp2, TRect(0,0,100,100), TRect(0,0,100,100), 1.0, false ); bmp->Canvas->EndScene(); |
This example shows how to draw a bitmap on to another canvas of a bitmap with explanations of parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
TBitmap *bmp=new TBitmap(200,200); TBitmap *bmp2=new TBitmap(200,200); bmp->Canvas->BeginScene(); bmp->Canvas->DrawBitmap( bmp2, // this is source bitmap TRect(0,0,100,100), // source rectangular area on source bitmap TRect(0,0,100,100), // destination rectangular area on destbmp 1.0, // opacity, it is successfully do transparent copying on to image false // if it is true it uses highspeed interpolation, lower quality ); bmp->Canvas->EndScene(); |
To do drawings on the Canvas of Bitmaps, please see details about How to Draw on Canvas in C++.
Mapping Bitmap to Analyze Pixels or Edit
We use Map to read or write pixels of bitmaps as below. In this example you need a Image component and a bitmap loaded in it.
1 2 3 4 5 6 7 8 9 |
TBitmapData bitmapData; if( Image1->Bitmap->Map(TMapAccess::ReadWrite, bitmapData)) // Lock bitmap and retrive bitmap data { ... // do operations, read/write pixels on Bitmap data here Image1->Bitmap->Unmap(bitmapData); } |
This example gets each pixels, adds Red, Green, Blue colors, and sets mean ARGB color to acr;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
TBitmapData bitmapData; unsigned long int R=0,G=0,B=0; TAlphaColorRec acr; int x,y,n=0; if( Image1->Bitmap->Map(TMapAccess::Read, bitmapData)) // Lock bitmap and retrive bitmap data { for (y=0; y<Image1->Bitmap->Height; y+=1) { for (x=0; x<Image1->Bitmap->Width; x+=1) { acr.Color = bitmapData.GetPixel (x, y); R+=acr.R; G+=acr.G; B+=acr.B; n++; } } Image1->Bitmap->Unmap(bitmapData); acr.R = R/n; acr.G = G/n; acr.B = B/n; acr.A = 255; } |