At the moment, you most likely have code in the event OnPaintfor TPaintBoxor the form itself. This code might look like this:
procedure TMyForm.PaintBox1Paint(Sender: TObject);
begin
with PaintBox1.Canvas do
begin
MoveTo(0, 0);
LineTo(42, 666);
// and so on.
end;
end;
We need to rephrase a little. We need to extract this paint code into a separate method. Pass this canvas method so that it is an agnostic of the canvas on which it draws.
procedure TMyForm.PaintToCanvas(Canvas: TCanvas);
begin
with Canvas do
begin
MoveTo(0, 0);
LineTo(42, 666);
// and so on.
end;
end;
procedure TMyForm.PaintBox1Paint(Sender: TObject);
begin
PaintToCanvas(PaintBox1.Canvas);
end;
, , . , :
procedure TMyForm.PaintToFile(const FileName: string);
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
Bitmap.SetSize(Paintbox1.Width, Paintbox1.Height);
PaintToCanvas(Bitmap.Canvas);
Bitmap.SaveToFile(FileName);
finally
Bitmap.Free;
end;
end;
, GIF, PNG, JPEG ..