A while back, I posed a solution for creating an adaptive GIF image from a JPEG image source in .NET, Use GDI+ to Save Crystal-Clear GIF Images with .NET
Recently a question was asked in the comments about how to save a 16-color GIF image using this library. It’s pretty simple actually, in fact, here’s the code:
13 Image img = Image.FromFile(“original_image.gif”);
14 OctreeQuantizer quantizer = new OctreeQuantizer(15, 4);
15 using ( Bitmap quantized = quantizer.Quantize ( img ) )
16 {
17 quantized.Save(“new_image.gif”, ImageFormat.Gif);
18 }
The OctreeQuantizer class’ constructor takes two parameters, the size of the color array (zero-based) and the bitdepth of the image. You can use this to create an image with varying palette sizes.
I thought that a quick downloadable example project would be in order, since there seems to be a lot of interest in saving gif images with adaptive palettes in .NET.