Solved

Add transparent PNG to TImageList at runtime in Delphi 2009

Posted on 2009-05-03
8
4,997 Views
Last Modified: 2013-11-23
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
Comment
Question by:DKropf3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 

Author Comment

by:DKropf3
ID: 24289727
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
 

Author Comment

by:DKropf3
ID: 24289958
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
 
LVL 2

Accepted Solution

by:
Morris_Szyslak earned 500 total points
ID: 24293228
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DKropf3
ID: 24293403
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
 
LVL 2

Assisted Solution

by:Morris_Szyslak
Morris_Szyslak earned 500 total points
ID: 24293654
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
 

Author Comment

by:DKropf3
ID: 24294997
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
 
LVL 2

Assisted Solution

by:Morris_Szyslak
Morris_Szyslak earned 500 total points
ID: 24295483
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
 
LVL 2

Assisted Solution

by:Morris_Szyslak
Morris_Szyslak earned 500 total points
ID: 24295519
Maybe you like a tool bar? Alpha seems to work there.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question