[SOLVED] TImagelist load at run time lazarus / 21-General ---------------------------------------------------------------------- torbente · 2021-01-16 00:14 I created a TImageList and loaded there all the glyphs for my buttons, using the inspector panel. Now i want remove the imagelist from the form and do it programatically. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GLyphFolder := 'gliphs/'; // the folder where i have the png filesMyGlyphs := TImageLits;// and to create and fill itMyGlyphs := Timagelist.Create(form1); MyGlyphs.Height:=16;MyGlyphs.Width:=16; and now? My glyphs files names are numerated, from 1.png to 26.png, so i could load all them with a simple for bucle... if i knew how... ---------------------------------------------------------------------- jamie · 2021-01-16 01:44 Create a Tbitmap and use that as the intermedia load from file.. And in a loop... ImageLIst.AddMasked(ThebitMapFromFile, theTransparentColor); if you don't have a transparent color, in other words it's a solid image... then ImageList.Add(TheBitMapFromFile, NIl); As for the file name you need to control the name using the IntTostr(ANumber)+'.bmp'; Etc ---------------------------------------------------------------------- Handoko · 2021-01-16 03:16 I wrote a simple demo, try it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls, Buttons, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    BitBtn1: TBitBtn;    BitBtn2: TBitBtn;    BitBtn3: TBitBtn;    BitBtn4: TBitBtn;    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  anImageList: TImageList;  anImage:     TImage;begin  Button1.Enabled    := False;   anImageList        := TImageList.Create(Self); // Let the form free it  anImage            := TImage.Create(nil);  anImageList.Width  := 60;  anImageList.Height := 67;   // Load pictures into image list  anImage.Picture.LoadFromFile('gloves.png') ;  anImageList.Add(anImage.Picture.Bitmap, nil);  anImage.Picture.LoadFromFile('hood.png') ;  anImageList.Add(anImage.Picture.Bitmap, nil);  anImage.Picture.LoadFromFile('robe.png') ;  anImageList.Add(anImage.Picture.Bitmap, nil);  anImage.Picture.LoadFromFile('sandals.png') ;  anImageList.Add(anImage.Picture.Bitmap, nil);   // Button1  BitBtn1.Images     := anImageList;  BitBtn1.ImageIndex := 0;  BitBtn1.Caption    := '';   // Button2  BitBtn2.Images     := anImageList;  BitBtn2.ImageIndex := 1;  BitBtn2.Caption    := '';   // Button3  BitBtn3.Images     := anImageList;  BitBtn3.ImageIndex := 2;  BitBtn3.Caption    := '';   // Button4  BitBtn4.Images     := anImageList;  BitBtn4.ImageIndex := 3;  BitBtn4.Caption    := '';   // Cleaning up  anImage.Free;  {anImageList.Free;}   // <--- Do not free the imagelistend; procedure TForm1.FormCreate(Sender: TObject);begin  BitBtn1.Caption := 'nothing';  BitBtn2.Caption := 'nothing';  BitBtn3.Caption := 'nothing';  BitBtn4.Caption := 'nothing';end; end. Quote from: torbente on January 16, 2021, 01:14:58 amNow i want remove the imagelist from the form and do it programatically. No, don't do it. Removing the image list will cause the images disappeared from the buttons. You can try it on the demo, it is on the line #77. I used TImage instead of TBitmap (as mentioned by jamie) because TBitmap cannot load png files directly. If the picture format is supported, you should use TBitmap, which is more lightweight. If you want to support transparent bitmap, which is not shown on the demo. You need to use Mask, which is already explained by jamie. Note: The demo is for showing how to load images to a image list and how to set the images to buttons. If it is my project, I will do it differently. Source of the pictures are from OpenGameArt, all are licensed in CC0. ---------------------------------------------------------------------- torbente · 2021-01-16 04:34 @handoko I mean i removed it from the form1, i want all my components to be created programatically at run time. @jamie Yes, i have transparent pngs for my buttons. Im trying to figure out a function that adds the image of the filename in the imagelist Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyGlyphs := TImageLits;Procedure AddImage(filename:string);var  srcBmp: TBitmap;  picture: TPicture;begin  Picture := TPicture.Create;  try    Picture.LoadFromFile(Filename);    SrcBmp := TBitmap.Create;    SrcBmp.Assign(Picture.Graphic);    MyGlyphs.Add(SrcBmp, nil);  finally    Picture.Free;  end;end; Function take from here: https://forum.lazarus.freepascal.org/index.php?topic=31915.0 But this not keeps the transparent color. ---------------------------------------------------------------------- lucamar · 2021-01-16 04:55 Quote from: torbente on January 16, 2021, 05:34:12 amBut this not keeps the transparent color. As Jamie explained, instead of: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     MyGlyphs.Add(SrcBmp, nil);you should use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     MyGlyphs.AddMasked(SrcBmp, MaskColor);where MaskColor is the color that should be regarded as "transparent". Only use TImageList.Add() if either: your images are fully opaque (using Nil as mask), oryou have two images: the normal one and a "mask" representing the transparent parts, like old style Windows icons. ---------------------------------------------------------------------- trev · 2021-01-16 05:48 Handoko's code adapted with ifdefs to also work under macOS: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} {$IFDEF Darwin}{$modeswitch objectivec1}{$ENDIF} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls, Buttons, ExtCtrls,  {$IFDEF Darwin}  CocoaAll,   // Needed for NSBundle  CocoaUtils  // Needed for NSStringToString  {$ENDIF}  ; type   { TForm1 }   TForm1 = class(TForm)    BitBtn1: TBitBtn;    BitBtn2: TBitBtn;    BitBtn3: TBitBtn;    BitBtn4: TBitBtn;    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } {$IFDEF Darwin}// Images are located in the application bundle's Resources directoryFunction GetPathForImageResource(Image: NSString) : string;begin  Result := NSS tringToString(NSBundle.mainBundle.pathForImageResource(Image));end;{$E NDIF} procedure TForm1.Button1Click(Sender: TObject);var  anImageList: TImageList;  anImage:     TImage;begin  Button1.Enabled    := False;   anImageList        := TImageList.Create(Self); // Let the form free it  anImage            := TImage.Create(nil);  anImageList.Width  := 60;  anImageList.Height := 67;   // Load pictures into image list  {$IFDEF Darwin}  anImage.Picture.LoadFromFile(GetPathForImageResource( NSStr('gloves.png')));  {$ELSE}  anImage.Picture.LoadFromFile('gloves.png');  {$ENDIF}  anImageList.Add(anImage.Picture.Bitmap, nil);  {$IFDEF Darwin}  anImag e.Picture.LoadFromFile(GetPathForImageResource(NSStr('hood.png')));  {$ELSE}  anImage.Picture.LoadFromFile('hood.png') ;  {$ENDIF}  anImageList.Add(anImage.Picture.Bitmap, nil);  {$IFDEF Darwin}  anImag e.Picture.LoadFromFile(GetPathForImageResource(NSStr('robe.png')));  {$ELSE}  anImage.Picture.LoadFromFile('robe.png') ;  {$ENDIF}  anImageList.Add(anImage.Picture.Bitmap, nil);  {$IFDEF Darwin}  anImag e.Picture.LoadFromFile(GetPathForImageResource(NSStr('sandals.png'))); {$ELSE}  anImage.Picture.LoadFromFile('sandals.png') ;  {$ENDIF}  anImageList.Add(anImage.Picture.Bitmap, nil);   // Button1  BitBtn1.Images     := anImageList;  BitBtn1.ImageIndex := 0;  BitBtn1.Caption    := '';   // Button2  BitBtn2.Images     := anImageList;  BitBtn2.ImageIndex := 1;  BitBtn2.Caption    := '';   // Button3  BitBtn3.Images     := anImageList;  BitBtn3.ImageIndex := 2;  BitBtn3.Caption    := '';   // Button4  BitBtn4.Images     := anImageList;  BitBtn4.ImageIndex := 3;  BitBtn4.Caption    := '';   // Cleaning up  anImage.Free;  {anImageList.Free;}   // <--- Do not free the imagelistend; procedure TForm1.FormCreate(Sender: TObject);begin  BitBtn1.Caption := 'nothing';  BitBtn2.Caption := 'nothing';  BitBtn3.Caption := 'nothing';  BitBtn4.Caption := 'nothing';end; end. ---------------------------------------------------------------------- GAN · 2021-01-16 05:51 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Buttons, ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    BitBtn1: TBitBtn;    BitBtn2: TBitBtn;    BitBtn3: TBitBtn;    ImageList1: TImageList;    procedure FormCreate(Sender: TObject);  private    procedure LoadImages;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  LoadImages;end; procedure TForm1.LoadImages;var  imagen:TImage;  i:Integer;begin  imagen:=TImage.Create(nil);  imagen.Picture.Clear;  ImageList1.Clear;   imagen.Picture.LoadFromFile(Application.Location+'1.png');  ImageList1.Insert(0,imagen.Picture.Bitmap,nil);  ImageList1.GetBitmap(0,BitBtn1.Glyph);  imagen.Picture.Clear;   imagen.Picture.LoadFromFile(Application.Location+'2.png');  ImageList1.Insert(1,imagen.Picture.Bitmap,nil);  ImageList1.GetBitmap(1,BitBtn2.Glyph);  imagen.Picture.Clear;   imagen.Picture.LoadFromFile(Application.Location+'3.png');  ImageList1.Insert(2,imagen.Picture.Bitmap,nil);  ImageList1.GetBitmap(2,BitBtn3.Glyph);  imagen.Picture.Clear;   FreeAndNil(imagen);end;  end.      Attached project. ---------------------------------------------------------------------- torbente · 2021-01-16 10:12 Quotewhere MaskColor is the color that should be regarded as "transparent". And how i could get that color? Somehow, imagelist detect it automatically when i add the files manually using the object inspector. Could be REALLY usefull: imagelist.AddFromFile.pngmasked(filename) // using top left pixel as transparent imagelist.AddFromFile.png(filename) imagelist.AddFromFile.bmp(filename) an so on... ---------------------------------------------------------------------- wp · 2021-01-16 10:45 Since your images are png there is no need to use TBitmap, better to use TPortableNetworkGraphic because it automatically handles the alpha channel transparency correctly. The TImageList.Add requires the images as a TCustomBitmap - this is the ancestor of TBitmap and TPortableNetworkGraphic. Therefore you can directly add pngs to the image list this way without any lossy conversions. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  png: TCustomBitmap;...  png := TPortableNetworkGraphic.Create;  try    png.LoadFromFile('images/7.png');    FImageList.Add(png, nil);  finally    png.Free;  end; The next point to be mentioned is that the idea of loading all images at runtimes requires that all images must be available when the program runs which makes distribution of the program much more complicated. It's better to add the images to the program resources which are linked into the binary, and to add them to the imagelist from there. Usually I create a resource file by using LazRes (which you maybe still have to compile from its sources in folder tools of your Lazarus installation). Here is a typical batchfile to create a resource "images.res" (you must adapt the path to LazRes.exe to your folder structure...): Code: [Select]set LAZRES_CMD=C:\Lazarus\lazarus- trunk_fpc-3.0.4\tools\lazres.exe dir /b *.png > filelist.txt %LAZRES_CMD% ../images.res @filelist.txt This creates a file images.res with all png files found in the current folder. In the resource file, the images are named like their files, but without the extension. When this batch file is stored in and executed from the images folder which I assume to be a subfolder of the Pascal source the images.res is written to the source folder. Then you must add a line {$R images.res} to the project file (don't add it to a second file!). Finally you call ImageList.AddResourceName with the name of the resource to add the image to the image list: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for i := 1 to 6 do    FImageList.AddResourceName(HINSTANCE, IntToStr(i));This assumes that the images are named '1', '2', ..., '6'. Please see the attached demo. ---------------------------------------------------------------------- jamie · 2021-01-16 17:10 Somewhere around here I need to find it, it's old Delphi documentation, that the lower left corner defines the transparent color if you use a default method of assignment, that is if the image is going to be transparent..  So when drawing transparent specifically Delphi uses the lower left corner for button icons etc..  So why does the TimageList follow that same rule here?  Maybe it does I don't know it but using the Imagelist.AddMasked(Image ,clDefault) to me would indicate to use the lower left corner  of the image.  using clNone means no mask and anything else should be what ever the color was specified.  I don't get the use of png's desire of using its alpha channel?  This most likely is the leading cause of why I have so much trouble populating a TimageList with proper transparency functionality... I always end up reworking the image to some different resolution to get it working , like 16 colors. ---------------------------------------------------------------------- wp · 2021-01-16 17:26 Don't ask me about the old Delphi method of pseudo-transparency, it never worked when I expected that it should. Why were there so many DrawTransparent painting routines out there to work around some issues? When I need transparency I use png images, they even can fade an image into the background which the old method cannot. Unlike old Delphi Lazarus can handle png in the same way as bmp. ---------------------------------------------------------------------- jamie · 2021-01-16 19:39 I never had any issues with the Delphi's implementation of the TImagelist as for Drawing the image transparently selectively.  The issues stem mostly from problems of loading random images and some pngs which I like to store due to size just do not work! I've had to use a paint program to convert them over to something simple like a BMP..  Don't worry on my account, I know how to work around … ---------------------------------------------------------------------- torbente · 2021-01-17 11:29 QuoteThe next point to be mentioned is that the idea of loading all images at runtimes requires that all images must be available when the program runs which makes distribution of the program much more complicated. So when a TImageList is placed in the form and filled using the GUI,it does something similar with those pictures (including the files data in the exe file as resources) ? I already tested with a simple project and i was able o run it separately without the images. ---------------------------------------------------------------------- wp · 2021-01-17 11:37 When you add an image to the ImageList in Lazarus design mode the image data are stored in the lfm file so that the original image is no longer needed. Although different in detail it is the same effect as working with resource files (in fact, the lfm is some kind of resource): the image data are stored inside the binary of the application. ---------------------------------------------------------------------- lucamar · 2021-01-17 11:56 Quote from: wp on January 17, 2021, 12:37:42 pmWhen you add an image to the ImageList in Lazarus design mode the image data are stored in the lfm file [...] which itself is included as a resource in the executable, so yes, it does "something similar" with the images ---------------------------------------------------------------------- torbente · 2021-01-18 06:09 Great, i got all it. Thanks to you all. I dont know if it belongs here, but it is related: I can use the images in the imagelist whit its transparency using: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Imagelist.GetBitmap(number,COntrol.Glyph); Not all the controls supports .glyph anyway, so how i could draw one glyph in the form (preserving transparency) that reacts to the mouse hover? I try with label, panel, statictext and others that can look clean, but they do not support .glyph.( xbutton do not fits my idea at all.) I guess it is something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TPicture.LoadfromImagelist(imagelistname, imagenumber); EDIT: Solved using GetIcon.