[Lazarus] Create transparency

Marc Weustink marc at dommelstein.net
Mon Jul 21 14:31:54 CEST 2008

Valdas Jank┼źnas wrote:
> Hello,
>   My code loads a PNG image from resource, modifies it, and then paints 
> on canvas:
> var
>    png: TPortableNetworkGraphic;
>    img: TLazIntfImage;
>    bmp: TBitmap;
> begin
>      // load from resource
>      png:=TPortableNetworkGraphic.Create;
>      png.LoadFromLazarusResource('a_png_image');
>      // prepare for modify
>      img:=png.CreateIntfImage;
>      png.Free;
>      // modyfy
>      ...... changing colors, setting alpha, ...
>      // prepare for draw
>      bmp:=TBitmap.Create;
>      bmp.LoadFromIntfImage(img);
>      img.Free;
>      // paint
>      ......
>   If image in resources has no alpha channel (img.HasTransparency 
> returns False in these cases), then in resulting image i get black 
> colour where i set colTransparent (in "modyfy" code block). How to 
> insert alpha channel (or maybe mask, [transparency==mask in recent 
> graphics changes?]) in existing image?

Before setting alpha values, the Rawimage description used by the 
IntfImage requires an Alpha channel. When setting full transpacency for 
a pixel, the value RGBA(0,0,0,0) is used. Without Alpha channel this 
results in a black pixel.

I don't have a fast (always working) way for adding an alpha channel. A 
slow way is creating a second image with a rgba description and copy the 
A faster way, (which might work in most cases) is when you have a 24bit 
depth description with a 32bits per pixel image. Then you can simply add 
an alpha description (precision=8, shift=24 or 0) to the rawimage.

Another thing, I'm not sure if loading form IntfImage/RawImage sets the 
right pixelformat (and thus image description).
Anyway, maybe the bmp is OK. How do you paint it ?


More information about the Lazarus mailing list