isa
isa

Reputation: 1085

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

The code below will extract icon from 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;

Upvotes: 7

Views: 5462

Answers (3)

Eugene Yokota
Eugene Yokota

Reputation: 95604

I found some libraries like PNG Delphi and Delphi PNG and MNG libraries after Googling.

Upvotes: 0

Uwe Raabe
Uwe Raabe

Reputation: 47694

The PngComponents contain a unit PngFunctions.pas, where you can have a look at

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

There you can find the code to convert a TIcon into a TPngImage - or just use that procedure.

Upvotes: 13

Stijn Sanders
Stijn Sanders

Reputation: 36840

Although TPngImage is no longer open source, if I take a quick look in an old copy I have lying around here, TPngImage.Assign only checks if the source is a TPngImage, and if not lets the 'default' assign do it's work, and for TBitmap or TGraphic, this will most probably use a plain draw on the canvas which throws away the transparency.

This looks like something to post on Delphi QC

Upvotes: -1

Related Questions