Board index » delphi » displaying dynamically created tImages?

displaying dynamically created tImages?

I'm new to Delphi, but liking it so far.  I'm left scratching my head on
something though.  I want to dynamically create and destroy TImages, so that
I can display and or overlay them.  The steps I've taken are create Image,
position Image, loadFromFile, and add to list.  The problem is that I can't
see any dynamically created images during run time.  (I can however see the
image if I assign it to a design time included image.)  I've tried
manipulating the visible property with no (obvious) effect.

Here's the code:

    anImage := TImage.Create (Form1);
    anImage.Top := SCREEN_ORIG_Y;    // these are just the constants that
define the display area of my form
    anImage.Left := SCREEN_ORIG_X;
//    anImage.Visible := true;
    try
      anImage.picture.loadFromFile (fileName);
    except
      on EInvalidGraphic do begin
        anImage.picture.graphic := nil;
      end;
    end;
    anImage.show;
    ImageList.add (anImage);
    image2.canvas.draw(0,0,anImage.picture.graphic);

Image2 is the design time image.  I'm not sure if the form should actually
be the owner of the dynamically created image, especially since I have the
ImageList.OwnsObjects property set.

TIA,
Jeremy Thien

 

Re:displaying dynamically created tImages?


at a cursory glance, should you not be refreshing the image?
You can do this by invalidating the owner of the image.

Quote
Jeremy Thien <jere...@adtecinc.com> wrote in message

news:3b4a35f5$1_1@dnews...
Quote
> I'm new to Delphi, but liking it so far.  I'm left scratching my head on
> something though.  I want to dynamically create and destroy TImages, so
that
> I can display and or overlay them.  The steps I've taken are create Image,
> position Image, loadFromFile, and add to list.  The problem is that I
can't
> see any dynamically created images during run time.  (I can however see
the
> image if I assign it to a design time included image.)  I've tried
> manipulating the visible property with no (obvious) effect.

> Here's the code:

>     anImage := TImage.Create (Form1);
>     anImage.Top := SCREEN_ORIG_Y;    // these are just the constants that
> define the display area of my form
>     anImage.Left := SCREEN_ORIG_X;
> //    anImage.Visible := true;
>     try
>       anImage.picture.loadFromFile (fileName);
>     except
>       on EInvalidGraphic do begin
>         anImage.picture.graphic := nil;
>       end;
>     end;
>     anImage.show;
>     ImageList.add (anImage);
>     image2.canvas.draw(0,0,anImage.picture.graphic);

> Image2 is the design time image.  I'm not sure if the form should actually
> be the owner of the dynamically created image, especially since I have the
> ImageList.OwnsObjects property set.

> TIA,
> Jeremy Thien

Re:displaying dynamically created tImages?


Are you setting the Parent property of your dynamically created TImages?

Quote
Jeremy Thien <jere...@adtecinc.com> wrote in message

news:3b4a35f5$1_1@dnews...
Quote
> something though.  I want to dynamically create and destroy TImages, so
that
> I can display and or overlay them.  The steps I've taken are create Image,
> position Image, loadFromFile, and add to list.  The problem is that I
can't
> see any dynamically created images during run time.  (I can however see
the
> image if I assign it to a design time included image.)  I've tried
> manipulating the visible property with no (obvious) effect.

Re:displaying dynamically created tImages?


Quote
Jeremy Thien wrote:
>     anImage := TImage.Create (Form1);
>     anImage.Top := SCREEN_ORIG_Y;    // these are just the constants that
> define the display area of my form
>     anImage.Left := SCREEN_ORIG_X;

Add .Width:= and .Height:= here to make sure these are not 0.
Additionally, add .Parent:=Form1, to actually position the image on the
form.

Quote
> //    anImage.Visible := true;
>     try
>       anImage.picture.loadFromFile (fileName);
>     except
>       on EInvalidGraphic do begin
>         anImage.picture.graphic := nil;
>       end;
>     end;
>     anImage.show;
>     ImageList.add (anImage);
>     image2.canvas.draw(0,0,anImage.picture.graphic);

> Image2 is the design time image.  I'm not sure if the form should actually
> be the owner of the dynamically created image, especially since I have the
> ImageList.OwnsObjects property set.

I do not really understand why you're using an image for loading and
drawing its picture onto another image afterwards. Are you aiming at
displaying the picture(s) at two different positions? If not, you might
want to use TPicture instead of the dynamically created TImage's.

Joris

Re:displaying dynamically created tImages?


Jeremy,

It is not entirely clear to me what you are attempting. If the
objective is to dynamically load a number of bitmaps in an imagelist
then you do not have to load them in a TImage first. Here is a little
demo which loads bitmaps in an imagelist and allows them to be viewed
in sequence.

procedure TForm1.Button1Click(Sender: TObject);
var
 bmp: TBitmap;
begin
  if OpenPictureDialog1.Execute then
  begin
    bmp:= TBitmap.Create;
    try
      bmp.LoadFromFile(OpenPictureDialog1.FileName);
      ImageList1.Add(bmp,nil);
    finally
      bmp.Free;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenPictureDialog1.Filter:= 'Bitmap files (*.bmp)|*.BMP';
  OpenPictureDialog1.InitialDir:= ExtractFilePath(Application.Exename);
  ShowImage:= 0;
  ImageList1.Height:= 180;
  ImageList1.Width:= 240;
end;

procedure TForm1.Button2Click(Sender: TObject);
var bmp: TBitmap;
begin
  if ImageList1.Count > 0 then
  begin
    bmp:= TBitmap.Create;
    try
      ImageList1.GetBitmap(ShowImage, bmp);
      Image1.Picture.Graphic:= bmp;
      if not(ShowImage = ImageList1.Count -1) then
        inc(ShowImage);
    finally
      bmp.Free;
    end;
  end;
end;

Should you want to dynamically create the TImage object (for viewing
not storage) do it like this.

Declare a field in under "private" in the form class declaration.

Image : TImage;

In the form's OnCreate handler.
  Image := TImage.Create(Self);
  with Image do
  begin
    Parent:= Self;
    Height:=
    Width:=
    Top:=
    Left:=
  end;

"Self" in this context means "Form1". Look up "Self" in the OLH for a
more detailed explanation. Setting "Parent" determines the visual
context in which the TImage is seen. (Here it is on the form, if you
had a panel on the form and wanted the Image contained in the panel
then substitute

Parent:= Panel1;

in the above. The Owner however remains the form.

Finally, there are circumstances where you would not assign an owner
(assign nil), and you *could* store a list of TImages in a TList, but I
think at this stage that would only mislead you. Think of a TImage as a
display tool and manipulate images in a TGraphic form (TBitmap in this
case) and you will be on the right lines.

Bob
----
Sent using Virtual Access 5.01 - download your freeware copy now
http://www.atlantic-coast.com/downloads/vasetup.exe

Re:displaying dynamically created tImages?


Thanks for the advice.  Now the dynamically created images are displaying,
but of course, I now have more questions.  How do I write event handlers for
the dynamically created images?  I want to drag and resize each image
independently.

Jeremy Thien

"Joris Van Damme" <as.van.damme.jo...@planetinternet.be> wrote in message
news:3B4A4D43.AA1E60E7@planetinternet.be...

Quote
> Jeremy Thien wrote:
> >     anImage := TImage.Create (Form1);
> >     anImage.Top := SCREEN_ORIG_Y;    // these are just the constants
that
> > define the display area of my form
> >     anImage.Left := SCREEN_ORIG_X;

> Add .Width:= and .Height:= here to make sure these are not 0.
> Additionally, add .Parent:=Form1, to actually position the image on the
> form.

> > //    anImage.Visible := true;
> >     try
> >       anImage.picture.loadFromFile (fileName);
> >     except
> >       on EInvalidGraphic do begin
> >         anImage.picture.graphic := nil;
> >       end;
> >     end;
> >     anImage.show;
> >     ImageList.add (anImage);
> >     image2.canvas.draw(0,0,anImage.picture.graphic);

> > Image2 is the design time image.  I'm not sure if the form should
actually
> > be the owner of the dynamically created image, especially since I have
the
> > ImageList.OwnsObjects property set.

> I do not really understand why you're using an image for loading and
> drawing its picture onto another image afterwards. Are you aiming at
> displaying the picture(s) at two different positions? If not, you might
> want to use TPicture instead of the dynamically created TImage's.

> Joris

Re:displaying dynamically created tImages?


Quote
Jeremy Thien wrote:
> How do I write event handlers for
> the dynamically created images?

Event properties are ordinary properties. Example...

  TForm1 = class(TForm)
  private
    FDynamicallyCreatedImage: TImage;
    procedure DynamicallyAssignedEventHandler(parameters and such);
  end;

...

  FDynamicallyCreatedImage:=TImage.Create(Form1);
  ...
  FDynamicallyCreateImage.OnSomeEvent:=
              Form1.DynamicallyAssignedEventHandler;
  ...

If the last stuff is inside a method of Form1, you can replace the first
reference to it with Self and the second reference can simply be
dropped. That'll get you code that is instance-independent.

You do want DynamicallyAssignedEventHandler to be declared correctly, of
course. One way is to use the help file to look at the exact declaration
you should use (if you don't remember it by heart). Another quick way is
to temporarily drop a TImage on your form at design time, use the object
inspector to create the event, delete the temp TImage, and move the
created event handler from the first section into a private section of
your form object declaration.

It seems that in your particular case you have a number of different
TImages created dynamically this way. You can use the tag property of
these TImage objects to give them all a unique identifying value and
access this value through the Sender parameter in the dynamically
assigned event handler. That way you can use a single handler (for a
single event of course, but) for any number of TImages.

I still don't quite understand what your actual goals are here. Some
flowchart design tool or something?

Joris

Other Threads