Jump to content

Load TW3Images from an array


Recommended Posts

I load all my images from the res/ folder into an array using the code from the ZenSky demo.

I have placed this code in the Application unit

 

 
    FImages:  Array of TW3Image; 
    FNames:   Array of String; 
    FCounter: Integer; 


(* number of images loaded into the cache *) 
  FCounter:=0; 

  (* names of images to load *) 
  FNames.add('res/onecent.jpg'); 
  FNames.add('res/fivecents.png'); 
  FNames.add('res/tencents.png'); 
  FNames.add('res/quarter.png'); 
  FNames.add('res/halfdollar.png'); 
  FNames.add('res/onedollar.png'); 

  (* create equal number of image elements *) 
  for x:=0 to FNames.length-1 do 
  FImages.add(TW3Image.Create(NIL)); 

(* initiate async loading on all *) 
  for x:=0 to FImages.Length-1 do 
  Begin 
    //FImages[x].OnLoad:=ImageLoaded; 
    FImages[x].LoadFromURL(FNames[x]); 
  end; 

 

 

I want to be able to assign the images to a TW3Image on another form. Example

 

img.LoadFromURL(TApplication(Application).FImages[0].ToDataUrl);

 

I dont get any errors, all i get is an outline of a TW3Image control

 

Shane

Link to post
Share on other sites
  • Administrators

Sorry about the late response.

 

Yes. It is possible to convert the images to Base64 strings and include them in your project.

Take a look at the TSmiley demo. Here we have created a dedicated class to show various smiley images, and all the smiley images are included as string constants.

 

...

15 minutes later

...

 

I got a bit carried away here, so I altered the TSmiley class to a TCoinImage class :-)

 

Now you can just create a TCoinImage class and set the denomination you would like to show.

(Oh. Please give me some feedback on the terminology. Is "Denomination" OK to describe "Value"?)

 

 
  FCoinImage: TCoinImage; 
  ... 
  FCoinImage := TCoinImage.Create(Self); 
  FCoinImage.SetBounds(40, 40, 100, 100); 
  FCoinImage.Denomination := Low(TCoinDenomination) + RandomInt(High(TCoinDenomination)+1); 

 

 

The images are represented like this (full code in demo below):

 
  CCoinImage: array [cdOneCent..cdOneDollar] of string = 
[ 
//** cdOneCent **// 
'data:image/png;base64,'+ 
'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAKPWlDQ1BpY2MAAHjanVNnVFPpFj33' + 
'3vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKO' + 
'g6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAiz' + 
'ZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBG' + 
... 
... 
... 

 

The "show image" magic:

 
  InnerHTML := '<img valign="middle" align="middle" alt="" ' + 'src="' + GetCoinImage + '" />'; 

 

 

Full project: https://www.dropbox.com/s/fpediv9bom6u5e6/TCoinImage%20%28based%20on%20TSmiley%29.zip

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...