Link to home
Start Free TrialLog in
Avatar of newind
newindFlag for Philippines

asked on

Print TForm.Canvas as Bitmap

I am developing a charting application.  I use the Canvas of TForm as my drawing area.  Is there a way for me to save my drawing as a bitmap file?

My current solution is to use BitBlt, but the problem with this is that when other windows popup over the Form which is being saved; they will also be included in the bitmap.  Furthermore, with BitBlt I cannot save the canvas of a Form when the form is minimized. How do I work on this?
ASKER CERTIFIED SOLUTION
Avatar of hubdog
hubdog

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hubdog
hubdog

tform.GetFormImage method can return a tbitmap

good luck
hubdog
            
You should try using a bitmap created in memory rather than the form's canvas to draw on. Saving the bitmap is far easier too.

These needs tidying up, but should give you a better idea...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Bitmap: TBitmap;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
      bitmap:= TBitmap.Create;
  bitmap.Width:= Clientwidth;
  bitmap.height:= clientheight;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bitmap.Free;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  bitmap.Canvas.LineTo(X,Y);
  canvas.Draw(0,0,bitmap);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  bitmap.SaveToFile('c:\temp\test.bmp');
end;

end.



Aubs
Does :
 
  myBmp := tBitmap.Create ;
  myBmp.Width := Form1.Width ;
  myBmp.Height := Form1.Height ;
  Form1.PaintTo( myBmp.Canvas.Handle, 0, 0 ) ;
 
work?

Good luck,

Tim.
Avatar of newind

ASKER

Well I say it's a great function.  It did solve my problem.

Thanks a lot hubdog!