Mirror

Setting the invisible color of a transparent image (Views: 301)


Problem/Question/Abstract:

How can I set the invisible color of a transparent image to other than the pixel in the lower-left corner?

Answer:

Transparent image

The TImage component has a Transparent property that when set to True displays the bitmap of the Picture property transparently. To do this, it takes the color of the bottom-leftmost pixel and treates all pixels of this color as invisible (you can see the objects begind the TImage). For example, this bitmap...

                GGGGGGGGG
                GRRRRRRRG
                GRRRRRRRG
                GRRRRRRRG
                GGGGGGGGG

...would be seen this way:


                 RRRRRRR
                 RRRRRRR
                 RRRRRRR


Since the pixel of the lower-left corner is green (G), all green pixels are made invisible. But, what if we wanted to see the image this way?

                GGGGGGGGG
                G       G
                G       G
                G       G
                GGGGGGGGG

TransparentColor and TransparentMode

To achieve this result, we have to set the TransparentColor and TransparentMode properties of the bitmap at run-time, for example when the form is created:

procedure TForm1.FormCreate(Sender: TObject);
begin
  with Image1.Picture.Bitmap do
  begin
    TransparentColor := clMaroon;
    TransparentMode := tmFixed;
  end;
end;

TransparentColor is the color (Color) that will be considered invisible. For example, we can use a constant (like $00800000, clMaroon or clBtnFace) or we can get the color from some pixel of the bitmap (like Canvas.Pixels[1,1]). TransparentMode is tmAuto by default, meaning it takes the color of the pixel in the lower-left
corner, and we have to set it to tmFixed (the other possible value) to tell the bitmap to use the color stored in the TransparentColor property as the invisible color.


Copyright (c) 2001 Ernesto De Spirito
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

<< Back to main page