Ok, I’m with ya now! I used the default GDI conversion from a JPG to a GIF and got the same nasty results you did. The good results I got before was because I was drawing lines and stuff on the canvas in plain colours, not loading a full-colour image.
Anyway, found a workable solution, just download the .net library attached below, put it in your /bin folder, and reference it in your project.
The use the following code to utilise the octree quantization method (this is VB):
Dim pic As New System.Drawing.Bitmap(Server.MapPath("myimage.jpg"))
Dim quantized As Bitmap = New ImageQuantization.OctreeQuantizer(255, 8).Quantize(pic)
Response.ContentType = "image/gif"
quantized.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif)
pic.Dispose()
Response.End()
I’ll have a crack at the c# code too, tho it’s not my forte’…
System.Drawing.Bitmap pic = new System.Drawing.Bitmap(Server.MapPath("myimage.jpg"));
Bitmap quantized = new ImageQuantization.OctreeQuantizer(255, 8).Quantize(pic);
Response.ContentType = "image/gif";
quantized.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
pic.Dispose();
Response.End();
Also attached is the GIF output it gave me, from a colour-gradated JPG, which turned out pretty good. I scored the library from this page, which also has the c# source for the quantization library. The library seems to use the Octree method you are after, as well as providing fixed-palette and greyscale quantizers as well.
All you need to do is upload the dll into your bin folder along with your own project dll.
Hope that helps! It sure helped me… I must thank you for posting that question, because it’s something I will actually be using now too!
But please remember, for all our sakes, cache your images to disk once you have created them, and reuse them where possible! This goes for whatever image tool you use, whether it’s this one, or AspImage or whatever. Please don’t create the same images on the fly on every page hit, otherwise you’ll monopolise server resources that we all have to share. :thumb:
ImageQuantization.zip (5.44 KB)