procedure SaveFormToFile(Sender: TForm; Filename: string);
var
bmp: TBitmap;
H, W: integer;
i: integer;
begin
bmp:=TBitmap.Create;
H:=0;
W:=0;
for i:=0 to Sender.ControlCount-1 do
begin
H:=MaxIntValue(H, Sender.Controls.Top+Sender.Controls.Height);
W:=MaxIntValue(W, Sender.Controls.Left+Sender.Controls.Width);
end;
H:=MaxIntValue(H, Sender.ClientRect.Bottom);
W:=MaxIntValue(W, Sender.ClientRect.Right);
// Get Height & Width that should be painted
bmp.Height:=H;
bmp.Width:=W;
Sender.PaintTo(bmp.Canvas.Handle, 0, 0);
bmp.SaveToFile(Filename);
bmp.Free;
end;