Populate ImageList at runtime
For some weird and wonderful reason, populating an image-list at runtime is not as straight-forward as I first imagined. If you want to create applications that loads resources on demand from disk (giving the user an option to change your graphics as well) then you really need to push stuff like images out of the main executable.
In my case I’m writing an IDE which is intended to compile with freepascal and Lazarus later on, so I try my best to avoid “Delphi centric” elements (read: components that only exist under Delphi), resource files and hard-coded data. Of which images are the number one enemy.
The problem
You would imagine that just loading a piece of graphics using TPicture, and then drawing it to the canvas of a bitmap – before inserting it into the imagelist, should be enough right? Well almost. The problem with good old TCanvas.draw() is that it could not care less about transparency information. Which is completely lost whenever you turn a TPicture.Graphic into a bitmap.
The solution, having messed around with this for at least 30 minutes, was simple: Use assign when moving the image from TPicture to TBitmap. That preserves the transparency information. Especially for PNG images which can also have an alpha map chunk.
And here is the result after having wasted 30 minutes trying different variations. The AddFromFile() function really should be included in TImageList. I cant imagine why this has not been included by Embarcadero. Either way, I have a clean and solid solution below:
The code
Here is the snippet source-code. Simply add it to your toolbox of snippets, or convert to a normal function if you like the old syntax style better.
Note: Remember to set your imagelist to
- ColorDepth: cd32Bit
- DrawingStyle: dsTransparent
- Masked: False
class function TQTXIDETools.LoadImageIntoList(aFileName:String; aList:TImageList; out aImageIndex:Integer):Boolean; var mImage: TPicture; mBitmap: TBitmap; Begin result:=False; aImageIndex:=-1; if aList<>NIl then begin if FileExists(aFilename,true) then begin mImage:=TPicture.Create; try (* Attempt to load image file *) try mImage.LoadFromFile(aFilename); except on exception do; end; (* image successfully loaded? *) if (mImage.Width>0) and (mImage.Height>0) then begin (* Draw image to a bitmap *) mBitmap:=TBitmap.Create; try (* copy pixels + transparency info to bitmap *) mBitmap.Assign(mImage.Graphic); (* Add bitmap to image-list, return index *) aImageIndex:=aList.add(mBitmap,NIL); result:=true; finally mBitmap.Free; end; end; finally mImage.Free; end; end; end; end;
Using the code
Using it is simplicity itself. Simply enter the form where you need images loaded, double-click on the “onCreate” event and call this procedure. You will ofcourse need to adapt the code to your own toolkit. I tend to isolate everything in classes, including paths, to make applications as modular and extendable as possible:
Procedure TfrmPreferences.setupImageList; var mAccess: TIDEAccessManager; mIndex: Integer; Begin if ideAccess.getIDEAccess(mAccess) then Begin (* Load "add" glyph *) with mAccess.PathManager do if TQTXIDETools.LoadImageIntoList (getImagePath('add.png'),imageList1, mIndex) then self.acAddPath.ImageIndex:=mIndex; (* Load "delete" glyph *) with mAccess.PathManager do if TQTXIDETools.LoadImageIntoList (getImagePath('delete.png'),imageList1, mIndex) then self.acDelPath.ImageIndex:=mIndex; end; end;
Leave a Reply Cancel reply
Recent
The vatican vault
- January 2022
- October 2021
- March 2021
- November 2020
- September 2020
- July 2020
- June 2020
- April 2020
- March 2020
- February 2020
- January 2020
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- February 2013
- August 2012
- June 2012
- May 2012
- April 2012
Hello
Thank you for that, much appreciated. I simplified the code somewhat (see here: https://pastebin.com/TTShg4q6) without losing any of the functionality from the callers PoV:
1. Removed many of the nested try blocks – any exception returns failure.
2. It’s pointless checking if the file exists – can still get an error when reading it (for example, no permissions to read) so I removed the check. Whether the file exists or not, the bevaiour remained the same.
3. The .Create() and .Free for both the objects are placed together – they will always be created/freed at the same time whether or not errors occur.
Once again, thank you for that snippet 🙂
You are welcome 🙂