Creating and Managing Off-screen Bitmaps

From RAD Studio
Jump to: navigation, search

Go Up to Off-screen Bitmaps


When creating complex graphic images, you should avoid drawing them directly on a canvas that appears onscreen. Instead of drawing on the canvas for a form or control, you can construct a bitmap object, draw on its canvas, and then copy its completed image to the onscreen canvas.

The most common use of an off-screen bitmap is in the Paint method of a graphic control. As with any temporary object, the bitmap should be protected with a try..finally block:

type
  TFancyControl = class(TGraphicControl)
  protected
    procedure Paint; override;                               { override the Paint method }
  end;
procedure TFancyControl.Paint;
var
  Bitmap: TBitmap;                        { temporary variable for the off-screen bitmap }
begin
  Bitmap := TBitmap.Create;                                { construct the bitmap object }
  try
    { draw on the bitmap }
    { copy the result into the control's canvas }
  finally
    Bitmap.Free;                                             { destroy the bitmap object }
  end;
end;