How can I convert PNG to GIF keeping the transparency?

1.1k views Asked by At

How can I convert PNG to GIF keeping the transparency?

I would have hoped that using the Assign( ) method would work but it doesn't seem to migrate transparency. In the GIF, it's represented as black.

    png:=TPngImage.Create;
    try
      png.LoadFromFile(sFile);
      // comes from file:  png.TransparencyMode;
      // comes from file:  png.Transparent

      // didn't help:  gif.Transparent:=true;

      gif.Assign(png);

      // didn't help:  gif.Transparent:=true;

      gif.SaveToFile('e:\tmp\out.gif');
    finally
      png.Free;
    end;

I haven't found a way to handle this in Delphi...

thanks!

1

There are 1 answers

3
David Heffernan On BEST ANSWER

It is possible to transfer an image from a PNG to a GIF. However, I don't recommend that you do so. The GIF format is substantially less capable than PNG. PNG supports RGBA color channels and partial transparency. GIF uses a 256 color palette and no support for partial transparency.

There are many libraries available that will make the best of a bad job and attempt to produce a GIF image that is close to the PNG image, but information will be lost.

The GIF format dates from the late 1980s and time has moved on. It has long since served its purpose. PNG is modern, capable and well supported. If it is possible to do so you should switch from GIF to using PNG.