Recent

Author Topic: Assign image from ImageList to Form.Icon  (Read 4856 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Assign image from ImageList to Form.Icon
« on: December 27, 2018, 07:57:28 pm »
Hi,
what is the correct (straightforward, simple) way to assign image from ImageList to Form.Icon? Now I use:
Code: Pascal  [Select][+][-]
  1. var aBMP: TBitmap;
  2. begin
  3.   aBMP:=TBitmap.Create;
  4.   ILMain.ResolutionForPPI[cIconWidth, 96, 1].GetBitmap(21, aBMP);  //ILMain=TImageList; 96=default DPI; 21=image index
  5.   FrmGraphPresets.Icon.Assign(aBMP);
  6.   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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Assign image from ImageList to Form.Icon
« Reply #1 on: December 27, 2018, 09:40:21 pm »
looks good here..
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Assign image from ImageList to Form.Icon
« Reply #2 on: December 27, 2018, 09:59:37 pm »
Yes, the code I posted works. I try to find something simpler. This
Code: Pascal  [Select][+][-]
  1. ILMain.GetIcon(13, FrmGraphPresets.Icon);
works but gives a memory leak.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Assign image from ImageList to Form.Icon
« Reply #3 on: December 27, 2018, 10:08:01 pm »
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.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Assign image from ImageList to Form.Icon
« Reply #4 on: December 27, 2018, 10:17:37 pm »
This does not work and gives memory leak:
Code: Pascal  [Select][+][-]
  1. var aIcon: TIcon;
  2. begin
  3.   aIcon:=TIcon.Create;
  4.   ILMain.GetIcon(13, aIcon);
  5.   FrmGraphPresets.Icon.Assign(aIcon);
  6.   aIcon.Free;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Assign image from ImageList to Form.Icon
« Reply #5 on: December 28, 2018, 12:13:59 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ImageList1.GetIcon(0, Icon);
  4. 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

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Assign image from ImageList to Form.Icon
« Reply #6 on: December 28, 2018, 12:57:42 am »
@ 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.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

stem

  • Jr. Member
  • **
  • Posts: 88
Re: Assign image from ImageList to Form.Icon
« Reply #7 on: April 26, 2019, 03:08:15 pm »
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

  • Hero Member
  • *****
  • Posts: 6090
Re: Assign image from ImageList to Form.Icon
« Reply #8 on: April 27, 2019, 01:42:08 am »
Please show us the relevant lines of code that is causing you hardship ?
The only true wisdom is knowing you know nothing

stem

  • Jr. Member
  • **
  • Posts: 88
Re: Assign image from ImageList to Form.Icon
« Reply #9 on: April 27, 2019, 09:54:00 am »
I've created a new project with the following code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ImageList1.GetIcon(0, Image1.Picture.Icon);
  4. end;
  5.  

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

  • Hero Member
  • *****
  • Posts: 6090
Re: Assign image from ImageList to Form.Icon
« Reply #10 on: April 27, 2019, 03:04:14 pm »
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.
The only true wisdom is knowing you know nothing

stem

  • Jr. Member
  • **
  • Posts: 88
Re: Assign image from ImageList to Form.Icon
« Reply #11 on: April 27, 2019, 03:28:00 pm »
Thank you for the hint, with the following code it works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ImageList1.GetBitmap(0, Image1.Picture.Bitmap);
  4. end;

stem

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Assign image from ImageList to Form.Icon
« Reply #12 on: April 27, 2019, 07:17:17 pm »
I've created a new project with the following code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ImageList1.GetIcon(0, Image1.Picture.Icon);
  4. end;
  5.  

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][+][-]
  1. procedure TMainForm.FormActivate(Sender: TObject);
  2. begin
  3.   {
  4.     ... code to check, set, or whatever some controls
  5.   }
  6.   OnActivate := Nil; { Ensure that it gets called just once }
  7. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018