1. Technology

Apply Custom Transparent Color for the TImage Delphi Control


Custom TImage Transparent Color

Custom TImage Transparent Color

The Transparent property of the TImage Delphi control specifies whether the objects behind the image should show through the background of the image.

When Transparent is True, by design, the bottom-left pixel and all the pixels of the same color as "invisible".

Custom Color TImage Transparency

If you do not want the color of the bottom-left pixel to specify which image color should be "transparent" - you can set the TransparentColor property programmatically.

Here's an example with 3 TImage controls all displaying the same bitmap image: red circled black x with magenta "surrounding".

Image 1 has Transparency set to False.

Image 2 has Transparency set to True.

Image 3 has Transparency set to True. Plus: TransparentColor and TransparentMode are programmatically set. The pixel used to set the transparent color is positioned in the middle of the image (original size 48 x 48 px.)

 procedure TForm1.FormCreate(Sender: TObject) ;
   Image1.Transparent := False;
   Image2.Transparent := True;
   Image3.Transparent := True;
   with Image3.Picture.Bitmap do
     //middle of the image
     TransparentColor := Canvas.Pixels[24,24];
     //actually not needed
     TransparentMode := tmFixed;
Note 1: If TransparentColor is assigned, the TransparentMode is automatically set to tmFixed so that the new transparent color can be used later. If you want TransparentColor to disregard any assignments and return the bottom left most pixel color again, set TransparentMode to tmAuto.

Note 2: the image used comes with Delphi, you can locate it in the Program Files\Common Files\Borland Shared\Images\GlyFX folder.

Delphi tips navigator:
» TFrame.OnCreate: how to Implement the OnCreate event for a Delphi TFrame object
« Display Custom Hints for TListView Sub Items

  1. About.com
  2. Technology
  3. Delphi
  4. Using VCL Components
  5. TImage
  6. Apply Custom Transparent Color for the TImage Delphi Control

©2014 About.com. All rights reserved.