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.

1
Drawing our Image

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

[C#]

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


2
System.Drawing Export

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

[C#]

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);
}


3
ImageGlue Color Reduction

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

[C#]

using (Bitmap bm = (Bitmap)Bitmap.FromFile(path)) {
  Effects.ReduceColors(bm, new ColorPaletteType(ColorPaletteFamily.Adaptive, 25), true);
  bm.Save(Server.MapPath("Optimizing_GIFs_NET_7_igreduced25.gif"));
}
using (Bitmap bm = (Bitmap)Bitmap.FromFile(path)) {
  Effects.ReduceColors(bm, new ColorPaletteType(ColorPaletteFamily.Adaptive, 10), true);
  bm.Save(Server.MapPath("Optimizing_GIFs_NET_7_igreduced10.gif"));
}


4
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.


laurentiu-iordache-qstGMhWuORE-unsplash.jpg


Optimizing_GIFs_NET_7_gdireduced.gif [118 KB]


Optimizing_GIFs_NET_7_igreduced25.gif [60 KB]


Optimizing_GIFs_NET_7_igreduced10.gif [32 KB]