AbcPdf pdf to gif conversion with keeping transparency

687 views Asked by At

I need to convert PDF into GIF with transparency. I've found some example of code on the official site, but result doesn't match expected. The common issue is transparency loosing.

The code I tried below:

using (var doc = new Doc()) {
            doc.Read(source);
            doc.Rendering.SaveAlpha = true;
            // the following lines from the official site. 
            // And this is showing blue background if I set this. 
            // But I don't need this blue background. 
            // Do not set anything special won't give good result.

            //doc.Color.SetRgb(0, 0, 255); // blue background ...
            //doc.FillRect(); // ... so you can see transparency

            doc.Rendering.Save(destination);
            doc.Clear();
        }

Please help if anyone have an expierence with this. Thanks

1

There are 1 answers

1
Lax Loafer On

The Rendering.SaveAlpha property doesn't apply to GIFs.

Color definitions in a GIF file are stored in palettes, not as channels. A palette may contain up to 256 colors, including one color set to transparent. Unlike using alpha channels there are no degrees of transparency. Each pixel will be either an opaque color or transparent.

To preserve the alpha channel you'll need to render to another format such as PNG, BMP, TIFF (grayscale, RGB and CMYK), or Photoshop PSD. If the results look good, have a go at converting it to a transparent GIF but I expect you'll find some transparency info will be lost. It's unavoidable.