Without using BGRA library would be as the next, using it, I dont know.
unit unit3;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, IntfGraphics, FPimage;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
function CrearBitmap(pControl : TCustomControl):TPortableNetworkGraphic;
procedure CrearPng(FileName: String; Transparencia: TColor);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function TForm1.CrearBitmap(pControl : TCustomControl):TPortableNetworkGraphic;
var
tmp: TPortableNetworkGraphic;
r: TRect;
begin
r:= Rect(0, 0, pControl.ClientWidth, pControl.ClientHeight);
tmp:= TPortableNetworkGraphic.Create;
tmp.PixelFormat := pf32bit;
tmp.SetSize(pControl.ClientWidth, pControl.ClientHeight);
tmp.Canvas.CopyRect(r, pControl.Canvas, r);
result:= tmp;
end;
procedure TForm1.CrearPng(FileName: String; Transparencia: TColor);
var
pantalla: TPortableNetworkGraphic;
m1: Tlazintfimage;
tempcolor: TFPColor;
i,j : Integer;
begin
pantalla := CrearBitmap(Form1);
m1:= pantalla.CreateIntfImage;
for i:=0 to m1.width -1 do
for j:=0 to m1.height -1 do
begin
tempcolor:=m1.colors[i,j];
if tempcolor= TColorToFPColor(Transparencia) then
tempcolor.alpha:=0
else
tempcolor.alpha:=$FFFF;
m1.colors[i,j]:= tempcolor; // if you want keep RGB information
end;
pantalla.LoadFromIntfImage(m1);
m1.free;
pantalla.SaveToFile(FileName);
pantalla.free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
CrearPng(Edit1.Text,clWhite);
end;
end.
The form example is the next:
object Form1: TForm1
Left = 292
Height = 46
Top = 224
Width = 321
Caption = 'Form1'
ClientHeight = 46
ClientWidth = 321
LCLVersion = '1.1'
object Edit1: TEdit
Left = 16
Height = 23
Top = 18
Width = 176
Color = clWhite
TabOrder = 0
Text = '32bits.png'
end
object Button1: TButton
Left = 216
Height = 25
Top = 16
Width = 75
Caption = 'Button1'
OnClick = Button1Click
TabOrder = 1
end
end