Recent

Author Topic: [SOLVED} Copying existing TImage to a new Object's TImage component  (Read 207 times)

Davo

  • Full Member
  • ***
  • Posts: 143
Problem : I want to display on screen an image contained in a dynamically created object's TImage component where that image is copied from an existing image that was created at design time. Currently the copied image does not display. The problem may lie with the copying code used. I have tried various ways based on research of the Forum. Or maybe the problem lies elsewhere? I don't think that the image is hidden behind the two shape components because I have deliberately tried displaying the image below them.

The relevant form at design time is shown below.

The same form at run time is also shown below. It shows the display of shape and label components of the dynamically created objects but it does not display the required image component.

Relevant code segments are :
Code: Pascal  [Select][+][-]
  1. unit showThecards;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls, Windows;
  10.  
  11. type
  12.  
  13.  TShowCard = class
  14.     myShapeA : TShape;      // to hold a black bordered white rectangle
  15.     myShapeB : TShape;      // to hold a non-bordered white overlay rectangle
  16.     myValue : TLabel;       // to hold a card value e.g. A, K, 3, 10
  17.     mySuitImage : TImage;   // to hold a suit image i.e. symbol for Club, Heart, Diamond, Spade
  18.     constructor init;
  19.     destructor zeroCard;
  20.     procedure place(tp,lft:integer);  // to position a card corner on the form
  21.   end;
  22.  
  23. shwCrds = array[1..16] of TShowCard;
  24.  
  25.   { TfmCards }
  26.  
  27.   TfmCards = class(TForm)
  28.     imClub: Timage;
  29.     //    etc. etc.
  30.   end;
  31.  
  32. var
  33.   fmCards: TfmCards;
  34.   showAllTheCards : shwCrds;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TfmCards }
  41.  
  42. procedure TfmCards.FormShow(Sender: TObject);
  43. var i : longInt;
  44. begin
  45.   {build the display card corners}
  46.   for i := 1 to 16 do
  47.     begin
  48.       showAllTheCards[i] := TShowCard.Create();
  49.       showAllTheCards[i].init;
  50.     end;
  51.   {test display all card corners to show Ace of Spades}
  52.   for i := 1 to 16 do
  53.     with showAllTheCards[i] do
  54.       begin
  55.         myValue.caption := 'A';
  56.         mySuitImage.picture.assign(imSpade.picture);  //  is the problem here?
  57. //      mySuitImage.picture := imSpade.picture;                // other
  58. //      mySuitImage.picture.bitmap := imSpade.picture.bitmap;  // attempts
  59.         place(25,60*i - 30);
  60.     end;
  61.   {test remove the display of 3 card corners to visually split the display of
  62.    the four suits}
  63.   for i := 1 to 16 do
  64.     if i in [4,9,13] then
  65.     begin
  66.       with showAllTheCards[i] do
  67.         begin
  68.           myShapeA.hide;
  69.           myShapeB.hide;
  70.           myValue.hide;
  71.           mySuitImage.hide;
  72.         end;
  73.     end;
  74. end;
  75.  
  76. constructor TShowCard.init;
  77. begin
  78.   inherited;
  79.   myValue := TLabel.Create(fmCards);
  80.   myShapeA := TShape.Create(fmCards);
  81.   myShapeB := TShape.Create(fmCards);
  82.   mySuitImage := TImage.Create(fmCards);
  83.   with myShapeA do
  84.     begin
  85.       inherited;
  86.       parent := fmCards;
  87.       // etc. etc.
  88.     end;
  89.   with myShapeB do
  90.     begin
  91.       inherited;
  92.       parent := fmCards;
  93.       //  etc. etc.
  94.     end;
  95.   with myValue do
  96.     begin
  97.       inherited;
  98.       parent := fmCards;
  99.       // etc. etc.
  100.     end;
  101.   with MySuitImage do
  102.     begin
  103.       inherited;
  104.       enabled := true;
  105.       height := 26;
  106.       proportional := true;
  107.       width := 20;
  108.       transparent := true;
  109.       visible := true;
  110.     end;
  111. end;
  112.  
  113. procedure TShowCard.place(tp,lft:integer);
  114. begin
  115.   myShapeA.top := tp;
  116.   myShapeA.Left := lft;
  117.   myShapeB.top := tp + 3;
  118.   myShapeB.left := lft + 3;
  119.   myValue.Top := tp + 6;
  120.   myValue.left := lft + 5;
  121.   mySuitImage.top := tp + 6;
  122.   mySuitImage.left := lft + 20;
  123. end;
  124.  
  125. end.
  126.  

« Last Edit: July 21, 2026, 11:57:22 am by Davo »

Xenno

  • Full Member
  • ***
  • Posts: 163
    • BS Programs
Re: Copying existing TImage to a new Object's TImage component
« Reply #1 on: July 21, 2026, 10:42:08 am »
Code: Pascal  [Select][+][-]
  1.   ...
  2.   with MySuitImage do
  3.     begin
  4.       inherited;
  5.       parent := fmCards; // <--- missing this line?
  6.    ...
  7.  

Maybe also required to arrange Z-Order by code.
Lazarus 4.6, Windows 10, https://www.youtube.com/@bsprograms
If I want to share, I give. If I want money, I sell. I don't set traps.

Thaddy

  • Hero Member
  • *****
  • Posts: 19625
  • Glad to be alive.
Re: Copying existing TImage to a new Object's TImage component
« Reply #2 on: July 21, 2026, 11:33:27 am »
Use Assign()... That's all. I see that went wrong, but that should work.
Small project source? If need be in private.
« Last Edit: July 21, 2026, 11:37:54 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Davo

  • Full Member
  • ***
  • Posts: 143
Thank you Xenno and Thaddy for your replies. They are much appreciated.

Yes Xenno the missing line was the problem.

All good now.

 

TinyPortal © 2005-2018