Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi convert gif to png with transparency

I am trying to convert a gif to png, that's easy, but the problem is the result image is not transparent, also I would like to have in the png image the alpha channel.

This is my code:

procedure TForm1.Button1Click(Sender: TObject);
var
png: TPngImage;
 p : TPicture;
begin
 p := TPicture.Create;
 p.LoadFromFile('C:\temp\php.gif');
 png := TPngImage.CreateBlank(COLOR_RGB , 8, p.Width, p.Height);
 png.Canvas.Draw(0,0, p.Graphic);
 png.SaveToFile('C:\Windows\Temp\test.png');
end;

The new picture has the background black, should be transparent.

If I try to add the ALPHA in the constructor, is 100% transparent.

png := TPngImage.CreateBlank(COLOR_RGBALPHA , 8, p.Width, p.Height);
like image 333
Max1298492 Avatar asked Nov 28 '14 08:11

Max1298492


1 Answers

Since Delphi XE 2 GDI+ is supported, which offers real easy to use options for conversions.
You just need to create TGPImage providing the image file to load and save this image with the wished encoder, found by the desired mime type.

uses Winapi.GDIPAPI, Winapi.GDIPOBJ, Winapi.GDIPUTIL;

procedure TForm8.Button1Click(Sender: TObject);
var
  encoderClsid: TGUID;
  stat: TStatus;
  IMG: TGPImage;
begin
  IMG := TGPImage.Create('C:\temp\transparent.gif');
  try
    GetEncoderClsid('image/png', encoderClsid);
    stat := IMG.Save('C:\temp\transparent.png', encoderClsid, nil);
  finally
    IMG.Free;
  end;
  if (stat = Ok) then
    Showmessage('Success');
end;

examples for the mime types:

image/bmp
image/jpeg
image/gif
image/tiff
image/png
like image 61
bummi Avatar answered Oct 12 '22 13:10

bummi