Board index » delphi » Form canvas won't draw TImage object

Form canvas won't draw TImage object

Anyone out there that can help me.

I am trying to show an image (using a TImage object) on a form during
runtime but I am not having much luck.

Here is the code I am using:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    APicture : TPicture;       // Added by author
    AnImage : TImage;          // Added by author
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  (* Create picture and load bitmap *)
  APicture := TPicture.Create;
  APicture.LoadFromFile('c:\windows\circles.bmp');

  (* Create image and assign a graphic *)
  AnImage := TImage.Create(Form1);
  with AnImage do begin
    Height := 32;
    Width := 32;
    Picture := APicture;
    Show;
  end;
end;

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

procedure TForm1.Button1Click(Sender: TObject);
begin
  (* Show the image on the form *)
  Form1.AnImage.Show;   //  This image won't show up on the form! Any
ideas anyone?
end;

end.

Thanks for responding.
Danny.

 

Re:Form canvas won't draw TImage object


First, you have to make the form the PARENT of your TImage. (i.e.
APicture.Parent := Form1;

Also, when you create APicture, you set its owner to be Form1, so you
don't have to free it yourself. Anything with an owner will be freed by
that owner.

Finally, you don't need to SHOW the TImage. In fact, TImage doesn't have
a SHOW method, so you are really calling the Form's Show method, which
doesn't help (unless, of course, your form is hidden).

Graham

Quote
Danny & Julia wrote:

> Anyone out there that can help me.

> I am trying to show an image (using a TImage object) on a form during
> runtime but I am not having much luck.

> Here is the code I am using:

[SNIP]
Quote
> implementation

> {$R *.DFM}

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   (* Create picture and load bitmap *)
>   APicture := TPicture.Create;
>   APicture.LoadFromFile('c:\windows\circles.bmp');

>   (* Create image and assign a graphic *)
>   AnImage := TImage.Create(Form1);
>   with AnImage do begin
>     Height := 32;
>     Width := 32;
>     Picture := APicture;
>     Show;
>   end;
> end;

> procedure TForm1.FormDestroy(Sender: TObject);
> begin
>   AnImage.Free;
>   APicture.Free;
> end;

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>   (* Show the image on the form *)
>   Form1.AnImage.Show;   //  This image won't show up on the form! Any
> ideas anyone?
> end;

> end.

> Thanks for responding.
> Danny.

Re:Form canvas won't draw TImage object


Danny:

You can simplify the code to:

procedure TForm1.FormCreate(Sender: TObject);
begin
  AnImage := TImage.Create(self);
  with AnImage do begin
    Parent   := self; // Very important. Parent is responsible for displaying
a control.
    AutoSize := true;
    Picture.LoadFromFile('c:\windows\circles.bmp');
  end;
end;

That being said, if you still wanted to do it the way that you posted, the
code should be:

procedure TForm1.FormCreate(Sender: TObject);
begin
  (* Create picture and load bitmap *)
  APicture := TPicture.Create;
  APicture.LoadFromFile('c:\windows\circles.bmp');

  (* Create image and assign a graphic *)
  AnImage := TImage.Create(self); // Note, "self". using the instance variable
can
  with AnImage do begin           // cause problems when there are multiple
instances.
    Parent := self;
    // Height := 32;
    // Width  := 32;
    AutoSize := true
    Picture.Assign(APicture);
    // Show; // Unnecessary, and it reference the form's "Show" method anyway.
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // AnImage.Free; // Unnecessary if you've created it with a non-nil Owner.
  APicture.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  (* Show the image on the form *)
  // Form1.AnImage.Show; //  Unnecessary.
end;
--
Regards
Ralph (TeamB)
(No private e-mail replies, please, unless explicitly requested).
--

Quote
Danny & Julia wrote in message <3679EBC2.6...@xtra.co.nz>...

|I am trying to show an image (using a TImage object) on a form during
|runtime but I am not having much luck.
<deletia>
|type
|  TForm1 = class(TForm)
|    Button1: TButton;
|    procedure FormCreate(Sender: TObject);
|    procedure FormDestroy(Sender: TObject);
|    procedure Button1Click(Sender: TObject);
|  private
|    { Private declarations }
|    APicture : TPicture;       // Added by author
|    AnImage : TImage;          // Added by author
|  public
|    { Public declarations }
|  end;
<deletia>
|procedure TForm1.FormCreate(Sender: TObject);
|begin
|  (* Create picture and load bitmap *)
|  APicture := TPicture.Create;
|  APicture.LoadFromFile('c:\windows\circles.bmp');
|
|  (* Create image and assign a graphic *)
|  AnImage := TImage.Create(Form1);
|  with AnImage do begin
|    Height := 32;
|    Width := 32;
|    Picture := APicture;
|    Show;
|  end;
|end;
|
|procedure TForm1.FormDestroy(Sender: TObject);
|begin
|  AnImage.Free;
|  APicture.Free;
|end;
|
|procedure TForm1.Button1Click(Sender: TObject);
|begin
|  (* Show the image on the form *)
|  Form1.AnImage.Show; //  This image won't show up on the form! Any
|ideas anyone?
|end;
|
|end.

Other Threads