• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5216
  • Last Modified:

Add transparent PNG to TImageList at runtime in Delphi 2009

Hi!

I'd like to add a RGB/8bit/Alpha PNG to a TImageList at runtime in Delphi 2009. I really can't find a decent way to do this, however you can add PNGs at design time with built in editor.

And still there is a problem if you add a transparent PNG at designtime ... transparency is not working fine ... it is just a black background (on Main menu for example).

Thanks for helping me with this one.
0
DKropf3
Asked:
DKropf3
  • 4
  • 4
4 Solutions
 
DKropf3Author Commented:
Ok, one step forward i made ... (still can't add png at runtime) but if i add png at design time and set TImageList's ColorDepth to cd32Bit it works with mainmenu ... but not with speedbutton.
exit.bmp
0
 
DKropf3Author Commented:
Ok almost solved by me ... will still grant 500 points to the one solving problem with TBitBtn and TSpeedButton.

See picture below ...

Solution for:
1. TimageList runtime add PNG with alpha:
---
ImageList.ColorDepth:=cd32Bit;
...
aPNG:=TPNGImage.Create;
try
 aPNG.loadfromfile('test.png');

    aBMP:=TBitmap.Create;
    try
     aBMP.Assign(aPNG);
     ImageList.Add(aBMP, nil);
    finally
     aBMP.Free;
    end;

finally
 aPNG.Free;
end;
---

Works with TMainMenu ---

the same stuf does not work with
tbitbutton and tspeedbutton

i tried:
   SpeedButton1.Glyph:=TBitmap.Create;
   SpeedButton1.Glyph.PixelFormat:=pf32Bit;
   and assign aPNG like above but result is in folowing picture ...

Please solve this ...

exit2.bmp
0
 
Morris_SzyslakCommented:
Try using a TAction, TActionList resp.
Reference your ImageList to ActionList.Images.
Create an Action and define the ImageIndex, and reference the Action in your SpeedButtons and BitButtons. You can also use the Action for your menu Items, but you have to reference the ImageList in the MainMenu.Images. The Glyphs and Methods are all handled by the Action. This works great for me.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DKropf3Author Commented:
Thanks Morris - your comment resulted in step forward to solution ...

I think before Alpha was completely lost ... now (purple ... see below) is not lost - it's there - but it is ignored .... :)

What now? And why it is ok on mainmenu ...

exit3.bmp
0
 
Morris_SzyslakCommented:
It looks like there are two colors around the actual glyphs. The outer color is used as transparent color, the inner color is displayed. Maybe this is an effect of the format conversion from png to bitmap or a scaling effect. Is the glyph of the same size as the ImageList?

Some things that are interesting to know:
Is it the same when you add the png at design time?
How does the glyph look in the image list editor?
Do you use the same image list for the main menu and for the buttons/actions?
0
 
DKropf3Author Commented:
Morris,

that color around the glyph is alpha transparency in .png image. I use the same imgelist for mainmenu and actionlist. It is the same if i add it designtime or runtime.

Still ok on mainmenu or if i copy it from ImageList to TImage.

exit4.bmp
cancel-16.png
0
 
Morris_SzyslakCommented:
TBitButton and TSpeedButton seem to have problems with alpha blending. The "old" transparency with selecting a certain color as transparent color does work however. Therefore the outer area where alpha is 100% transparent is chosen as transparent color, the area directly around the image is partly transparent and doesn't match the transparent color and is painted.

You could either
-use glyphs without alpha transparency or
-try to find / develop a fix for TBitButton and / or TSpeedButton or
-use a component that works like TBitButton or TSpeedButton and can handle alpha.
0
 
Morris_SzyslakCommented:
Maybe you like a tool bar? Alpha seems to work there.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now