[Lazarus] create bng with transparent background
ryansmithhe at gmail.com
Wed Jun 10 14:52:00 CEST 2020
On 2020/06/10 13:57, Luca Olivetti via lazarus wrote:
> is there a way to create a png with transparent background?
> I tried the suggestion here
> to set the brush to bsClear but it doesn't work.
> I also tried with a TFPMemoryImage using either a brush or a pen with
> alphaTransparent but I couldn't have a transparent png, the background
> is always black.
The brush.style:=bsClear merely dictates how any next additive drawing
will fill its specific background area, for instance if you were to add
Text to the image it will draw the text without a background, which is
useful if you wish to write on pictures or transparent PNGs.
I think the best way to make a transparent PNG currently, is to do what
you did, but then paint at least the upper left corner pixel with a
transparency of 1 and not Zero, which you still won't see, however, may
I suggest using the very useful BGRA Bitmap package? Then it simply
becomes a question of:
... BGRABitmap, BGRABitmapTypes, ...
bmp : TBGRABitmap;
png : TPortableNetworkGraphic;
bmp := TBGRABitmap.Create( neededWidth, neededHeight );
png := TPortableNetworkGraphic.Create;
bmp.FillRect( bmp.ClipRect, BGRAPixelTransparent, dmSet );
png.Assign( bmp );
Done. Obviously the real advantage of those BGRA tools is when you
actually wish to draw stuff on the image.
Also, how do you know the output PNG file is not transparent? I ask
because windows and windows viewers will sometimes show a perfectly
transaprent PNG with a Black/White background. The only safe way to know
is to open it in Paint or GIMP or such. (Linux typically doesn't lie
like that, but any specific viewer may of course still do it some
arbitrary way. A graphic editor is obliged to show the real content.)
Note: That BGRAPixelTransaprent is simply an internally defined TPixel
with R G B and Alpha all set to 0, so you can also make up your own
transparent pixel and use that in stead.
More information about the lazarus