CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

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 


Published Dec 08 2006, 01:26 AM by Brendan Tompkins
Filed under:

Comments

vbnetdiscuss said:

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

plz.

# March 5, 2007 10:11 PM

pravat said:

Your code doesn't work in 2 , 4, and some times 8 colors.. how can do it ?

I also really deserve it.

# March 6, 2007 4:26 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!