Yeah, I know I'm kind of late to the party, but it might help somebody out eventually... In fact I was looking for an answer, stumbled upon this question, wasn't very satisfied and then whipped up this hunk o' junk.
Key ingredient here is to configure the FPWriter before actually using it: you have to specify you want the alpha channel.
Code to determine if there IS an alpha channel could use some work I believe-- I haven't yet found a graceful way to do it except checking if it's in use or not. This does the trick, using FPimage:
procedure Rommel;
type TReadKlas = class of TFPCustomImageReader;
var
image: TFPCustomImage;
Ireader: TFPCustomImageReader;
IWriter: TFPWriterPNG;
Reader : TReadKlas;
ex : string;
MM : TMemoryStream;
OK,Transp : Boolean;
x,y : integer;
BLO : Word;
begin
Image := TFPMemoryImage.Create(8,
;
EX:=ExtractFileExt(filspec);
if leftstr(ex,1)='.' then delete(ex,1,1);
ex:=upcase(EX);
if ex='BMP' then reader:=TFPReaderBMP else
if ex='GIF' then reader:=TFPReaderGIF else
if (ex='JPG') OR (Ex='JPEG') then reader:=TFPReaderJPEG else
if ex='PCX' then reader:=TFPReaderPCX else
if ex='PNG' then reader:=TFPReaderPNG else
if ex='PNM' then reader:=TFPReaderPNM else
if ex='PSD' then reader:=TFPReaderPSD else
if (ex='TGA') OR (Ex='TARGA') then reader:=TFPReaderTarga else
if (ex='TIFF') OR (ex='TIF') then reader:=TFPReaderTIFF else
if ex='XPM' then reader:=TFPReaderXPM else
if ex='XWD' then reader:=TFPReaderXWD else
exit;
IReader:=Reader.Create;
MM:=TMemoryStream.Create; OK:=FALSE;
try
MM.writeBuffer(Bla[1],length(Bla));
MM.Position:=0;
Image.LoadFromStream(MM,IReader);
OK:=TRUE;
except
on E:Exception do writeln(E.Message);
end;
MM.Free;
iReader.Free;
BLO:=224 SHL 8;
if OK then begin
Transp:=FALSE;
y:=0; while (y<image.height) AND NOT(Transp) do begin
x:=0; while x<image.width do begin
if image.Colors[x,y].alpha<BLO then begin Transp:=TRUE; break; end;
inc(x);
end;
inc(y);
end;
if transp then writeln('Image has transparency.') else writeln('Image is solid.');
writeln(inttostr(image.Width)+' x '+inttostr(image.height));
IWriter := TFPWriterPNG.Create;
IWriter.UseAlpha:=Transp;
IWRiter.CompressionLevel:=clMax;
Image.SaveToFile('C:\BROL.PNG',IWriter);
IWriter.Free;
end;
image.Free;
end;