Solved

Add transparent PNG to TImageList at runtime in Delphi 2009

Posted on 2009-05-03
8
4,958 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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. …
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.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

828 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