Short Version
Fill in the fuction:
procedure LoadImageListMasked(AImageList: TImageList; hbmp: HBITMAP; TransparentColor: TColor);
var
bmp: Graphics.TBitmap;
begin
bmp := Graphics.TBitmap.Create;
bmp.Handle := hbmp;
bmp.Transparent := True;
bmp.TransparentMode := tmFixed;
bmp.TransparentColor := TransparentColor;
AImageList.AddMasked(bmp, TransparentColor);
bmp.Free;
end;
Long Version
I have a handle (hbmp
) to a 256-color bitmap:
I want to load this image in a (Delphi 5) TImageList, using the clFuchsia
as the mask color:
var
bmp: TGraphics.TBitmap;
bmp := TBitmap.Create;
bmp.Transparent := True; //Default: False
bmp.TransparentMode := tmFixed; //Default: tmAuto
bmp.TransparentColor := clFuchsia; //Default: $02FF00FF
bmp.Handle := hbmp;
ImageList1.Clear;
ImageList1.Height := bmp.Height;
ImageList1.Width := bmp.Height;
ImageList1.BkColor := clNone; //Default: $1FFFFFFF (clNone)
ImageList1.AddMasked(bmp, clFuchsia);
Except when I actually use the images in the image list (on a TToolbar say), the clFuchsia
color isn't masked off:
What am i doing wrong?
Options Grid
There are various options available to be played with:
bmp.TransparentMode
: [tmAuto
,tmFixed
] (Default:tmAuto
)bmp.TransparentColor
: TColor (Default: -1)bmp.Transparent
: Boolean (Default: False)ImageList1.BkColor
: TColor (Default:clNone
)
Lets try every combination i can think of:
TransparentMode | TransparentColor | BkColor | Result |
---|---|---|---|
tmAuto (default) | -1 (default) | clFuchsia | Fail |
tmAuto (default) | -1 (default) | clNone (default) | Fail |
tmFixed | clFuchsia | clNone (default) | Fail |
tmAuto (default) | -1 (default) | clNone (default) | then assign the handle: |
tmAuto (default) | $02FF00FF (auto) | clNone (default) | Fail |
tmAuto (default) | -1 (default) | clNone (default) | then assign the handle: |
tmAuto (default) | $02FF00FF (auto) | clNone (default) | then change Mode to tmFixed: |
tmFixed | $02FF00FF (auto) | clNone (default) | Fail |
tmAuto (default) | -1 (default) | clNone (default) | then assign the handle: |
tmAuto (default) | $02FF00FF (auto) | clNone (default) | then change mode to tmFixed: |
tmFixed | $02FF00FF (auto) | clNone (default) | then change TransparentColor to clFuchsia: |
tmFixed | $02FF00FF (clFuchsia) | clNone (default) | Fail |
After tracing through the VCL i realized there is another property of TBitmap:
TBitmap.Transparent: Boolean
It defaults to False. I've now also tried setting it to True.
Don't set the
TImageList.BkColor
, leave it at its default ofclNone
. You are telling theTImageList
to draw its masked bitmaps onto theTToolbar
over a fixed color. That is why yourTToolBar
is showing fuchsia. It is theTImageList.BkColor
being shown, not theTBitmap.TransparentColor
.Also, just as an FYI...
Don't set the
TBitmap.TransparentMode
property totmAuto
if you want a specificTransparentColor
.Setting the
TransparentColor
property to a value other thanclDefault
will set theTransparentMode
property totmFixed
. Then setting theTransparentMode
back totmAuto
will set theTransparentColor
back tocrDefault
, thus losing your color choice.Though, it shouldn't really matter, as internally
AddMasked()
creates a newTBitmap
copied from the sourceTBitmap
, and it will set the copied bitmap'sTransparentColor
to the inputTColor
you specify, so you don't actually need to make your sourceTBitmap
be transparent at all.