Assign image from ImageList to Form.Icon lazarus / 31-LCL ---------------------------------------------------------------------- Blaazen · 2018-12-27 18:57 Hi, what is the correct (straightforward, simple) way to assign image from ImageList to Form.Icon? Now I 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 var aBMP: TBitmap;begin  aBMP:=TBitmap.Create;  ILMain.ResolutionForPPI[cIconWidth, 96, 1].GetBitmap(21, aBMP);  //ILMain=TImageList; 96=default DPI; 21=image index  FrmGraphPresets.Icon.Assign(aBMP);  aBMP.Free;which works. Is there any other way which doesn't need temporary bitmap? I tried other methods of TImagList but it didn't work or gave memory leaks. Thanks, V. ---------------------------------------------------------------------- jamie · 2018-12-27 20:40 looks good here.. ---------------------------------------------------------------------- Blaazen · 2018-12-27 20:59 Yes, the code I posted works. I try to find something simpler. This 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 ILMain.GetIcon(13, FrmGraphPresets.Icon);works but gives a memory leak. ---------------------------------------------------------------------- jamie · 2018-12-27 21:08 Most likely took ownership of the icon and try to delete a already deleted icon on program shutdown. using the Assign simply makes a copy of the image. ---------------------------------------------------------------------- Blaazen · 2018-12-27 21:17 This does not work and gives memory leak: 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 aIcon: TIcon;begin  aIcon:=TIcon.Create;  ILMain.GetIcon(13, aIcon);  FrmGraphPresets.Icon.Assign(aIcon);  aIcon.Free; ---------------------------------------------------------------------- wp · 2018-12-27 23:13 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 procedure TForm1.Button1Click(Sender: TObject);begin  ImageList1.GetIcon(0, Icon);end; This works for me without a memory leak: Laz trunk r59910/fpc 3.0.4 (32 bit) on Win 10/64 bit. Note the new trunk version - there were a couple of TImageList fixes regarding memory leaks recently. ---------------------------------------------------------------------- Blaazen · 2018-12-27 23:57 @ wp This code works on Qt but gives leak. It does not work on GTk2 + gives leak too. Lazarus 2.1.0 r59916 FPC 3.3.1 x86_64-linux-qt I'll write a bugreport. ---------------------------------------------------------------------- stem · 2019-04-26 13:08 Hi, I also have a project where I have trouble with GetIcon. This ImageList procedure works fine with Windows 10, but does not work with Linux Gtk2 (Lazarus 2.0.2). Have there been replies to your bugreport? Stefan ---------------------------------------------------------------------- jamie · 2019-04-26 23:42 Please show us the relevant lines of code that is causing you hardship ? ---------------------------------------------------------------------- stem · 2019-04-27 07:54 I've created a new project with the following code: 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 procedure TForm1.FormCreate(Sender: TObject);begin  ImageList1.GetIcon(0, Image1.Picture.Icon);end;  As you can see in the screenshots, the Windows 10 version displays the image, the Linux Mint Xfce version does not display it. Windows: Lazarus 2.0.0 Linux: Lazarus 2.0.2 stem ---------------------------------------------------------------------- jamie · 2019-04-27 13:04 Its possible you have a widget set issue there because it should work however, I am sure if you used the ImageList.Draw(….. , it will image your icon. That is what I normally do, I first ensure the icon/bitmap has the area defined then draw on it from a Imagelist. ---------------------------------------------------------------------- stem · 2019-04-27 13:28 Thank you for the hint, with the following code it works: 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 procedure TForm1.FormCreate(Sender: TObject);begin  ImageList1.GetBitmap(0, Image1.Picture.Bitmap);end; stem ---------------------------------------------------------------------- lucamar · 2019-04-27 17:17 Quote from: stem on April 27, 2019, 09:54:00 amI've created a new project with the following code: 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 procedure TForm1.FormCreate(Sender: TObject);begin  ImageList1.GetIcon(0, Image1.Picture.Icon);end;  As you can see in the screenshots, the Windows 10 version displays the image, the Linux Mint Xfce version does not display it. Maybe OT, but a recommendation: Instead of adding that kind of code (code that accesses controls in the form) to the OnCreate handler add it to an OnActivate handler, to ensure that the controls are fully initialized and ready. I've been bitten by this more than one time so now I alwasy do 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 procedure TMainForm.FormActivate(Sender: TObject);begin  {    ... code to check, set, or whatever some controls  }  OnActivate := Nil; { Ensure that it gets called just once }end;