When reducing the colors of a bitmap which has an alpha channel (i.e. when some portions of the bitmap are transparent), you may need to keep the transparency location. The article below describes how you can do this in Graphics Mill.
There are two approaches to preserving transparency:
To keep transparency in indexed bitmaps one of palette entries is marked with the transparent flag. All transparent pixels should be equal to the index of this entry. This approach does not allow an image to have semi-transparent entries, which can result in artifacts on the edges, between opaque and transparent areas of the image. However there are several advantages with this approach regardless of the drawbacks. The palette size is quite small and usually each palette entry is important; GIF file format for example supports only one transparent entry in a palette.
To decide if a semi-transparent pixel will be fully transparent or opaque, the transparency treshold is used. You can set it via the ColorManagementProvider.TransparentIndexThreshold or the ColorConverter.TransparentIndexThreshold property. This value defines the minimum alpha channel value for pixels which should be considered as opaque. If a pixel has a smaller alpha channel value it becomes fully transparent.
The following threshold values are frequently used:
255
.1
. If you set it to 0
, there will be no transparent pixels.10
to 16
.Here is a code example that demonstrates the first method of preserving transparency:
using (var bitmap = new Bitmap(@"Images\in.png")) { bitmap.ColorManagement.TransparentIndexThreshold = 128; bitmap.ColorManagement.Palette = ColorPalette.Create(bitmap, 32); bitmap.ColorManagement.Convert(PixelFormat.Format8bppIndexed); bitmap.Save(@"Images\Output\out.png"); }
The following code snippet uses the ColorConverter transform to present the transparency:
using (var bitmap = new Bitmap(@"Images\in.png")) using (var converter = new ColorConverter()) { converter.TransparentIndexThreshold = 128; converter.Palette = ColorPalette.Create(bitmap, 32); converter.DestinationPixelFormat = PixelFormat.Format8bppIndexed; using (var newbitmap = converter.Apply(bitmap)) newbitmap.Save(@"Images\Output\out.png"); }
NeuQuant is a palette generation algorithm. It creates an optimal palette for a given image preserving alpha transparency. So, it allows converting real-color images with transparency to indexed PNG images almost without transparency information lost.
To use NeuQuant you should set a ColorPaletteType.NeuQuant palette as a ColorManagementProvider.Palette. The following snippet uses the NeuQuant algorithm:
using (var bitmap = new Bitmap(@"Images\in.png")) { bitmap.ColorManagement.Palette = new ColorPalette(ColorPaletteType.NeuQuant); bitmap.ColorManagement.Convert(PixelFormat.Format8bppIndexed); bitmap.Save(@"Images\Output\out.png"); }