2012-07-22 47 views
6

我想從TImageList加載一張圖片(32位深度,透明)到TImage。標準方法是ImageList.GetBitmap(Index, Image.Picture.Bitmap);。但是GetBitmap方法不適用於透明度,所以我總是得到一個不透明的位圖。如何從ImageList加載透明圖像?

+1

像['RRUZ suggested'(http://stackoverflow.com/questions/10972986/png-image-from-imagelist#comment14330479_10973026)前一段時間... – TLama 2012-07-22 12:03:33

回答

23

解決方法非常簡單 - ImageList提供了另一種方法,GetIcon,該方法在透明度下工作正常。代碼加載一個透明的形象將是:

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

而且不要忘記設置正確的ImageList屬性:

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

我也有各種問題,在圖像通過從一個的TImageList。所以我有一個簡單的包裝例程,通常可以完成這項工作,並且它會強制執行透明度。下面的代碼是Delphi 2005,imlActiveView是帶有我的按鈕字形圖像集的tImageList組件。

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; 

這裏是第5個imlActiveView圖像傳遞到btnNavigate.Glyph的使用示例。

LoadBitmap (5, btnNavigate.Glyph)