14

I want to load a PNG with transparent background in a TImageList and use it in a TMainMenu (via TAction).
However, in Delphi the background is just black.

This guy says it worked for him, but with a button instead of a main menu.

My setup:

  • My image is 8 bit per color plus 8 bit for transparency channel (I have tried also with 1 bit for transparency but still doesn't work). The image looks just fine in any other program.
  • Delphi XE.
  • In TImageList's editor, the 'transparent color' and 'Fill color' are set to 'Default' and are disabled.
Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • 1
    Hi RRuz. I use a TImageList. It has a DrawingStyle properties and it can only be set to 'transparent' (which is still not working). But there is not cd32bit. – Gabriel Aug 13 '11 at 14:08
  • 2
    Sorry I mean the property `ColorDepth` must be setted to `cd32Bit` – RRUZ Aug 13 '11 at 14:09
  • Update: there is a ColorDepth property but if I set it to 'cd32Bit' the images are not painted anymore in the menu. – Gabriel Aug 13 '11 at 14:16
  • 4
    You have to reload the images when you change the colordepth. It is the same as changing width or height. I have no problem with this setting on Windows 7, even when DrawingStyle is set to normal. – Uwe Raabe Aug 13 '11 at 14:28
  • Yes. It looks so. The thing is that Delphi deleted ALL images without prior notice. I don't have them save on my drive. Lucky that I could recover the original DFM file from 'history'. – Gabriel Aug 13 '11 at 14:40
  • 3
    @Altar when you change some properties of the TimageList component like (ex: Width, Height or ColorDepth) the images are removed from the component this is how the TImagelist works. Ayway returning to your original issue you must the set ColorDepth property of the TimageList to `cd32Bit` and then load the images which `must be in 32 bpp png format.` – RRUZ Aug 13 '11 at 14:55
  • 3
    "a lot of work would have been lost"... Surely you are using a source version control system? – Marjan Venema Aug 13 '11 at 16:43
  • Seems the BIG DELPHI BUG is just unexpected (to some) but well known (to others) behaviour. – Uwe Raabe Aug 13 '11 at 17:17
  • 1
    @Uwe: indeed. This is known behaviour. – Rudy Velthuis Aug 13 '11 at 18:17
  • 1
    @Uwe - I was just consistent and never had to change the the size or color depth on my icons. – Gabriel Aug 15 '11 at 12:22

3 Answers3

13

There's no reason to backup whole DFM file.

Before doing something to your ImageList you should export your images: click the "Export" button in the image list editor and it will save all images to a single .BMP file.

After changing properties: ColorDepth = cd32Bit, DrawingStyle = dsTransparent

You should click "Add" button and select the file you previously exported. Then, images will be drawn with transparency in the menu and toolbars (on D2010 at least)... No need to use 3rd party components.

agf
  • 171,228
  • 44
  • 289
  • 238
djsoft
  • 1,051
  • 8
  • 19
  • 1
    This button can do 2 things. 1) If you have nothing selected in the "Images" list, it will export all images in the imagelist. 2) If one or more icons selected, it will export only selected images. – djsoft Aug 14 '11 at 10:04
  • How do you "select nothing" in the "Images" list? When i double-click the `TImageList` component the first image is always initially selected, and clicking the **Export** button will insist on only exporting that one image. How do you export an entire imagelist? – Ian Boyd Oct 14 '14 at 15:36
  • 1
    @IanBoyd - click at the very bottom of icons list, right above the scroll bar. – djsoft Dec 06 '14 at 19:36
8
  1. Set the following property values for TImageList:
  • DrawingStyle: dsTransparent
  • ColorDepth: cd32Bit
  • TImageList's editor -> set the 'transparent color' to True
  1. In "Project Settings" under "Application" set "Enable runtime themes".

Update for Delphi Rio:

TImageList must have:

  • BkGColor = clNone
  • BlendColor = clNone,
  • ColorDepth = cd32bit,
  • DrawingStyle = dsTransparent (or) dsNormal,
  • Masked = true.

The images can be loaded from a PNG with transparent bkg color.


Note: Editing some properties of TImageList will delete (without any notice) all existing images in your list. Backup your images (or DFM) first.

Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • Delphi XE is almost (see below) ready to work with alpha channel PNG according to your link. But you must know how to work with TImageList editor. The way we use is to have all images backed up in a directory and numbered, e.g. 000_image1.png, 001_image2.png and so on. Then on every change we just clear the image list and reload with images from this directory, it can be done with one Add... command and images are ordered as they were before. However, there is one (today trivial) problem: without enabled runtime themes the PNG transparency does not work in Delphi XE. – tk_ Oct 07 '15 at 10:16
6

I had been using TPngComponents since Delphi 7. That provide very good png support for most of the delphi build-in / third party components. Embarcadero had purchased another well know Open Sources PNG Supporting component know as TPngImage and build into Delphi's native graphic support in later Delphi versions. So when I changed to Delphi 2010, I think Delphi had native support now and test out the native support but find that the alpha support is not good enough.

So I had used back that TPngComponents again and it had better support for most components, including main menu. I think that package should be usable to DXE with limited modification.

edit : Thanks for Uwe Raabe for pointing out the product Codegear (now Embarcadero) purchased was another component TPngImage rather then the TPngComponent.

Justmade
  • 1,496
  • 11
  • 16
  • 1
    Ok. Thanks. So it is a Delphi bug. I will go back to the good old BMP format. Long live Delphi and the 'modern' GUI interfaces we can create with it. – Gabriel Aug 13 '11 at 13:59
  • PngComponents was not bought by CodeGear (now Embarcadero). That was only the PNG support from Gustavo Daud. PngComponents is and has always been a component packages based on PNG support created by Martijn Saly. The current version is valid for D2009 and higher. – Uwe Raabe Aug 13 '11 at 14:22
  • @Uwe Rabbee Oh I checked and yes I mixed up TPngImage with TPngComponent. Detail of Codegear (now Embarcadero) can be find in [Nick Hodges Blog](http://blogs.embarcadero.com/nickhodges/2008/08/13/39100). No matter why Delphi native support is different from the TPNGComponent which I was using. Thanks for pointing that out and I will modify my message. – Justmade Aug 13 '11 at 14:44
  • D7 compatible [PngComponents 1.0. RC3 from 2005-06-29](https://web.archive.org/web/20071026151635/http://www.thany.org/article/32/PngComponents), which also can convert PNG to ICO. – AmigoJack Jan 05 '23 at 00:33