How to load a transparent Image from ImageList?

本秂侑毒 提交于 2019-12-21 03:54:15

问题


I want to load a picture (32 bit-depth, transparent) from a TImageList to an TImage. The standard approach would be ImageList.GetBitmap(Index, Image.Picture.Bitmap);. However the GetBitmap method doesn't work with transparency, so I always get a non-transparent bitmap.


回答1:


The workaround is rather simple - ImageList offers another method, GetIcon, which works OK with transparency. Code to load a transparent Image would be:

ImageList.GetIcon(Index, Image.Picture.Icon);

And don't forget to set proper ImageList properties:

ImageList.ColorDepth:=cd32bit;
ImageList.DrawingStyle:=dsTransparent;



回答2:


I too have had various issues with passing in images from the a tImageList. So I have a simple wrapper routine that generally does the job and it enforces the transparency. The code below is Delphi 2005 and imlActiveView is the tImageList component that has my set of button glyph images.

procedure TfrmForm.LoadBitmap (Number : integer; bmp : tBitMap);
var
  ActiveBitmap : TBitMap;
begin
  ActiveBitmap := TBitMap.Create;
  try
    imlActiveView.GetBitmap (Number, ActiveBitmap);
    bmp.Transparent := true;
    bmp.Height      := ActiveBitmap.Height;
    bmp.Width       := ActiveBitmap.Width;
    bmp.Canvas.Draw (0, 0, ActiveBitmap);
  finally
    ActiveBitmap.Free;
  end
end;

Here is an example of use where the 5th imlActiveView image is passed into the btnNavigate.Glyph.

LoadBitmap (5, btnNavigate.Glyph)


来源:https://stackoverflow.com/questions/11600044/how-to-load-a-transparent-image-from-imagelist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!