How to convert icon to png with alpha transparency in delphi?

The code below will extract the icon from the file and convert it to png, but without alpha transparency?

var IconIndex : word; icon:TIcon; png:TPngImage; bmp:TBitmap; begin IconIndex := 0; icon := TIcon.Create; icon.Handle := ExtractAssociatedIcon(hInstance,pChar(Edit1.Text), IconIndex) ; bmp:= TBitmap.Create; bmp.LoadFromFile('blank.bmp'); DrawIcon(bmp.Canvas.Handle, 0, 0, icon.Handle) ; png := TPngImage.Create(); png.Assign(bmp); png.SaveToFile('icon.png'); end; 
+4
source share
3 answers

PngComponents contains the PngFunctions.pas block, where you can see

procedure ConvertToPNG(Source: TGraphic; out Dest: TPngImage);

Here you can find the code to convert TIcon to TPngImage - or just use this procedure.

+11
source

I found several libraries such as PNG Delphi and Delphi PNG and MNG after Googling.

0
source

Although TPngImage is no longer open source , if I quickly look at the old copy that I was lying here, TPngImage.Assign only checks if the source is TPngImage, and if this does not allow to assign 'default', this works, and for TBitmap or TGraphic this will most likely use a simple canvas drawing that discards transparency.

This is similar to Delphi QC post

-1
source

All Articles