Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2020-06

create bng with transparent background

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

Hello,

is there a way to create a png with transparent background?

I tried the suggestion here

http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-How-to-create-a-transparent-PNG-td4047716.html

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.

Regards

Luca Olivetti Wetron Automation Technology http://www.wetron.es/ Tel. +34 93 5883004 (Ext.3010) Fax +34 93 5883007

Reply by email

R.Smith
to Luca Olivetti
Luca Olivetti wrote 13 lines
Hello, is there a way to create a png with transparent background? I tried the suggestion here http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-How-to-create-a-transparent-PNG-td4047716.html 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:

uses
  ... BGRABitmap, BGRABitmapTypes, ...

var
   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.

Good luck!

Reply by email

Luca Olivetti
to R.Smith

El 10/6/20 a les 14:52, R.Smith via lazarus ha escrit:

Show quoted text 5 lines
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:

Yes, that's what I finally did, but it's overkill considering that I just need to generate a circle with a specified color.

Show quoted text 7 lines
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.)

I loaded them in firefox (which is the intended usage since the application is a webserver).

Bye

Luca Olivetti Wetron Automation Technology http://www.wetron.es/ Tel. +34 93 5883004 (Ext.3010) Fax +34 93 5883007

Reply by email