One of the features notably lacking in GDI+ is the ability to color reduce images. This is particularly important if you want to export optimized GIF images.

This example shows you how to use ImageGlue to color reduce a GIF image using an adaptive palette and then export the result. Using this combination you get the LZW compression of GDI+ and the sophisticated color reduction capabilities of ImageGlue.

 

   
1
 
Drawing our Image    
     

We create our ImageGlue canvas and draw our image onto it. We then create a reference to a GDI+ Bitmap and save it for later use.

[Visual Basic]
Dim ca As New Canvas
Dim bm As Bitmap
ca.DrawFile(Server.MapPath("rez/astro.jpg"), "")

[C#]
Canvas ca = new Canvas();
Bitmap bm;
ca.DrawFile(Server.MapPath("rez/astro.jpg"), "");

 

   
2
 
Default Export    
     

So we have a comparison we'll start by exporting the image using the color reduction available in GDI+. We copy the canvas into our GDI+ Bitmap and then export it.

[Visual Basic]
bm = ImageCopy.MakeBitmap(ca)
bm.Save(Server.MapPath("astro1.gif"), ImageFormat.Gif)

[C#]
bm = ImageCopy.MakeBitmap(ca);
bm.Save(Server.MapPath("astro1.gif"), ImageFormat.Gif);

 

   
3
 
Palletized Export    
     

Now we color reduce the canvas down to 32 colors using an ImageGlue adaptive palette. We then copy the canvas into the GDI+ Bitmap and export it again.

[Visual Basic]
ca.ReduceColors("adaptive", 25, true)
bm = ImageCopy.MakeBitmap(ca)
bm.Save(Server.MapPath("astro2.gif"), ImageFormat.Gif)

[C#]
ca.ReduceColors("adaptive", 25, true);
bm = ImageCopy.MakeBitmap(ca);
bm.Save(Server.MapPath("astro2.gif"), ImageFormat.Gif);

 

   
4
 
Tidying Up    
     

Finally we release any memory being used by our objects.

[Visual Basic]
bm.Dispose()
ca.Clear()

[C#]
bm.Dispose();
ca.Clear();

 

   
5
 
Sample Output    
     

Sample output images are shown below with respective file sizes. You can see that the color reduced image has been exported at a significantly reduced file size with little loss of quality.


astro1.gif [36.4 KB]


astro2.gif [18.1 KB]