Delphi coding hints and tips.

Clearing a TBitmap

Unfortunately there is no Clear method within the TBitMap class. I have found when using the TBitmap class a Clear mehtod would be very useful sometimes. But in fact there is a simple way to clear a bitmap, just assign nil to it, as the following code shows:

procedure BitmapClear(const Bitmap: Graphics.TBitmap);
begin
  Bitmap.Assign( nil );
end;

Above is a function that clears a TBitmap by calling assign with a nil source.

If you have a look at VCL source code for the TBitmap Assign method you will see what calling TBitmap.Assign( nil ) does. First it will first blank the currently held bitmap with FillChar. Then it call's it's private method NewImage creating a new bitmap of size 0 by 0 pixel.