オフスクリーン ビットマップの作成と管理
オフスクリーン ビットマップ への移動
複雑なグラフィック画像を作成する場合は、画面に表示されているキャンバスに直接描画しないようにします。フォームやコントロールのキャンバスに描画するのではなく、ビットマップ オブジェクトを作成し、そのキャンバスに描画してから、できあがった画像を画面上のキャンバスにコピーします。
オフスクリーン ビットマップは、グラフィック コントロールの Paint メソッドで最もよく使用されます。どのような一時オブジェクトでも同様ですが、次のように、ビットマップを try..finally ブロックで保護します。
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;