One of the features notably lacking in GDI+ is the ability to optimize color reduction for 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.

Drawing our Image

We create our ImageGlue bitmap and draw our image onto it.


Bitmap bitmap = new Bitmap(Server.MapPath("rez/astro.jpg"));

System.Drawing Export

So we have a comparison we'll start by exporting the GIF image using System.Drawing.


string path = Server.MapPath("rez/laurentiu-iordache-qstGMhWuORE-unsplash.jpg");
using (System.Drawing.Bitmap bm = (System.Drawing.Bitmap)SD.Bitmap.FromFile(path)) {
  bm.Save(Server.MapPath("Optimizing_GIFs_NET_7_gdireduced.gif"), SD.Imaging.ImageFormat.Gif);

ImageGlue Color Reduction

Now we color reduce the bitmap down to twenty-five colors using the ImageGlue adaptive palette. And again down to ten.


using (Bitmap bm = (Bitmap)Bitmap.FromFile(path)) {
  Effects.ReduceColors(bm, new ColorPaletteType(ColorPaletteFamily.Adaptive, 25), true);
using (Bitmap bm = (Bitmap)Bitmap.FromFile(path)) {
  Effects.ReduceColors(bm, new ColorPaletteType(ColorPaletteFamily.Adaptive, 10), true);

Input and 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.


Optimizing_GIFs_NET_7_gdireduced.gif [118 KB]

Optimizing_GIFs_NET_7_igreduced25.gif [60 KB]

Optimizing_GIFs_NET_7_igreduced10.gif [32 KB]