GIF Image Color Reduction in .NET

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.

See this post here for the latest on this project 

About Brendan Tompkins

Brendan runs CodeBetter.Com and Devlicio.Us. He is a former MVP for Microsoft .NET and is president of Port Technology Services, a partner with Port Solution Integrators a provider of hardware and software integration services for the transportation and logistics industry.
This entry was posted in ASP.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Suma_Muddappa

    Hi Brendan,

    I’ve couple of images which i need to do save as to a different location. Any pointers on this will be very helpful. Also where can i find the Mindroute.Core.dll

  • pravat

    Your code doesn’t work in 2 , 4, and some times 8 colors.. how can do it ?
    I also really deserve it.

  • http://www.machinelevel.com vbnetdiscuss

    What about color reduction as 2,4,and 8 color Gif image saving?
    plz.