views:

149

answers:

3

Hi again,

I've spended hours for this (simple) one and don't find a solution :/

I'm using D7 and the TImageList. The ImageList is assigned to a toolbar. When I populate the ImageList at designtime, the icons (with partial transparency) are looking fine. But I need to populate it at runtime, and when I do this the icons are looking pretty shitty - complete loose of the partial transparency.

I just tried to load the icons from a .res file - with the same result. I've tried third party image lists also without success. I have no clue what I could do :/ Thanks 2 all ;)

edit:

To be honest I dont know exactly whats going on. Alpha blending is the correkt term... Here are 2 screenies:

Icon added at designtime: alt text

Icon added at runtime: alt text

Your comment that alpha blending is not supported just brought the solution: I've edited the image in an editor and removed the "alpha blended" pixels - and now it looks fine. But its still strange that the icons look other when added at runtime instead of designtime. If you (or somebody else ;) can explain it, I would be happy ;) thanks for you support!

A: 

I had the exact same problems a couple of years ago. It's a Delphi problem. I ended up putting the images in the list at design time, even though I really didn't want to. I also had to use a DevExpress image list to get the best results and to use 32 bit color images.

Jeremy Mullin
A: 

As Jeremy said this is indeed a Delphi limitation.

One work around I've used for images that I was putting onto buttons (PNGs with alpha transparency in my case) is to store the PNGs as resources, and at run time paint them onto a button sized bitmap filled with clBtnFace. The bitmap was then used as the control's glyph.

Delphi's built in support for icons with alpha masks is very limited, however there's an excellent icon library kicon which may help.

glob
+1  A: 

To support alpha transparency, you need to create the image list and populate it at runtime:

function AddIconFromResource(ImageList: TImageList; ResID: Integer): Integer;
var
  Icon: TIcon;
begin
  Icon := TIcon.Create;
  try
    Icon.LoadFromResourceID(HInstance, ResID);
    Result := ImageList.AddIcon(Icon);
  finally
    Icon.Free;
  end;
end;

function AddPngFromResource(ImageList: TImageList; ResID: Integer): Integer;
var
  Png: TPngGraphic;
  ResStream: TStream;
  Bitmap: TBitmap;
begin
  ResStream := nil;
  Png := nil;
  Bitmap := nil;
  try
    ResStream := TResourceStream.CreateFromID(HInstance, ResID, RT_RCDATA);
    Png := TPNGGraphic.Create;
    Png.LoadFromStream(ResStream);
    FreeAndNil(ResStream);
    Bitmap := TBitmap.Create;
    Bitmap.Assign(Png);
    FreeAndNil(Png);
    Result := ImageList.Add(Bitmap, nil);              
  finally
    Bitmap.Free;
    ResStream.Free;
    Png.Free;
  end;
end;

// this could be e.g. in the form's or datamodule's OnCreate event
begin
  // create the imagelist
  ImageList := TImageList.Create(Self);
  ImageList.Name := 'ImageList';
  ImageList.DrawingStyle := dsTransparent;
  ImageList.Handle := ImageList_Create(ImageList.Width, ImageList.Height, ILC_COLOR32 or ILC_MASK, 0, ImageList.AllocBy);
  // populate the imagelist with png images from resources
  AddPngFromResource(ImageList, ...);
  // or icons
  AddIconFromResource(ImageList, ...);

end;
TOndrej
Hey,what delphi version are you using? I have delphi7 and an TIcon has no function "LoadFromResourceID"...And could you showme the ImageList_Create function?Btw. which resource editor do you use?thx ;)
ben
The code is for Delphi 2007. TIcon.LoadFromResourceID does this: Icon.Handle := LoadIcon(Instance, MakeIntResource(ResID));ImageList_Create is a Windows API (common controls, comctl32.dll) declared in CommCtrl unit.For a resource editor, I sometimes use resxplor from Delphi demos. :-)
TOndrej