I am using ImageSharp to save a half transparent image into GIF, but gif is saved with white background, no transparency.
var gifEnc = new SixLabors.ImageSharp.Formats.Gif.GifEncoder();
edited.Save(cache, gifEnc);
Gif Output, white background, instead of transparent:


Encouraged by your confirmation in the comments now I'm adding a non-ImageSharp solution. Instead, it uses my GIF Encoder from this NuGet package.
1. Obtain an
IReadableBitmapDatainstance from your imageAssuming you work with ImageSharp images:
If performance is crucial you can try to spare copying pixels and just access the image buffer directly by
DangerousTryGetSinglePixelMemoryif possible. You have a better chance if you load the image withnew Configuration { PreferContiguousImageBuffers = true }.Alternatively, if you are not tied to ImageSharp you can use one of the specialized packages of the directly supported frameworks. Then obtaining a bitmap data is as simple as follows:
2. Encode GIF
Once you have the bitmap data, this is really simple. Ideally, the solution is a single line:
For the 32bpp pixel format selected above the
EncodeImagemethod will use theWuquantizer with alpha threshold = 128. This must work as requested but if you want to adjust transparency you can pass a non-null quantizer to the method. And, if your sources are high color images you also may want to specify the ditherer parameter to preserve the details better. See more details about quantizing and dithering here.