Solved

Add transparent PNG to TImageList at runtime in Delphi 2009

Posted on 2009-05-03
8
4,893 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now