Search code examples
c++buildervclrad-studio

How to Clear a Bitmap in VCL


How to clear a bitmap that was declared in the .cpp as follows:

Graphics::TBitmap * Bitmap1 = new Graphics::TBitmap;

All the examples I find are for Firemonkey and there it seems quite simple

MyBitmap = new TBitmap(0,0);
...


MyBitmap->Clear(claWhite);

or

MyBitmap->ClearRect(MyRect);

But Clear() and ClearRect() are not members of TBitmap in VCL

I expect I should delete Bitmap1; in order to clear it, but then how to re-declare it, so that it is still global to all methods in the form?

Thanks in advance.


Solution

  • You don't need to delete and recreate the TBitmap. Simply draw a new image over top of it, for instance by using its Canvas->FillRect() method (that is essentially what the FMX examples are doing), eg:

    Bitmap1->Brush->Color = clWhite;
    Bitmap1->Canvas->FillRect(Rect(0, 0, Bitmap1->Width, Bitmap1->Height));