DsImaging provides a powerful API to handle various operations on image colors, such as setting the contrast level, adjusting levels of an image histogram, working with color channels and color quantization. These features help to manipulate the color attributes of an image.
DsImaging provides AutoContrast and AutoLevel methods in GrayscaleBitmap class and GcBitmap class respectively to improve the colors of an image. These methods modify color intensities such that the maximum range of values (0-255) is fully covered. They also clip the extremely high and low values, and correct the highlights and shadows of an image.
Original Image | Image after AutoLevel |
---|---|
To improve the colors of an image:
C# |
Copy Code
|
---|---|
public void SetContrast() { //Adjust contrast/level for GcBitmap GcBitmap bmp = new GcBitmap("Images/house.jpg"); bmp.AutoLevel(2f, 2f); bmp.SaveAsJpeg("autolevel_house.jpg"); } |
Levels adjustments are used to improve the tonal range and brightness levels of an image histogram. For this purpose, DsImaging library provides AdjustLevels method in both GcBitmap class and GrayscaleBitmap class.
Original Image | Image after AdjustLevels |
---|---|
To adjust levels of an image histogram:
C# |
Copy Code
|
---|---|
public void SetBrightness() { GcBitmap bmp = new GcBitmap("Images/house.jpg"); bmp.AdjustLevels(0, 0x00646464, 0x00969696, 0x00FAFAFA); bmp.SaveAsJpeg("brighthouse.jpg"); } |
The basic elements of a digital image are the pixels, which in turn are made up of color channels, or the primary colors. For example, the RGB color model has three separate color channels; one for red, one for green and one for blue. DsImaging provides two methods, ExportColorChannel and ImportColorChannel in the GcBitmap class. The ExportColorChannel method exports the specific color channel data from an image, whereas the ImportColorChannel method creates a colored image based on the specified color channel data.
Original Image | Blue Color Channel | Green Color Channel |
---|---|---|
To export Blue and Green color channels of a colored image:
C# |
Copy Code
|
---|---|
using (var bmp = new GcBitmap("Images/tudor.jpg")) { var gbmp = bmp.ToGrayscaleBitmap(ColorChannel.Blue); var outBmp = gbmp.ToGcBitmap(); outBmp.SaveAsJpeg("Images/blue.jpg"); bmp.ExportColorChannel(gbmp, ColorChannel.Green); gbmp.ToGcBitmap(outBmp, false); outBmp.SaveAsJpeg("Images/green.jpg"); outBmp.Dispose(); gbmp.Dispose(); } |
Original Image | Red Color Channel |
---|---|
To create an image based on its Red color channel:
C# |
Copy Code
|
---|---|
using (var bmp = new GcBitmap(Images/tudor.jpg)) using (var gbmpRed = bmp.ToGrayscaleBitmap(ColorChannel.Red)) { bmp.Clear(Color.Black); //Use the ImportColorChannel method for creating a color image from one of its grayscale channel bmp.ImportColorChannel(gbmpRed, ColorChannel.Red); bmp.SaveAsJpeg(Images/red.jpg); } |
Octree color quantization algorithm achieves color quantization by reducing the number of distinct colors used in an image while trying to
retain the visual appearance of the original image. The GenerateOctreePalette method of the GcBitmap class applies the octree color
quantization algorithm to a colored image for generating an octree color palette. This color palette is very useful in scenarios where the
device only supports limited number of colors, or when there is a need to reduce the color information of an image due to memory
limitations.
To apply Octree color quantization:
C# |
Copy Code
|
---|---|
using (GcBitmap bmp = new GcBitmap("Images/tudor.jpg")) { uint[] pal = bmp.GenerateOctreePalette(10); using (GcGifWriter gw = new GcGifWriter("Images/test.gif")) { gw.AppendFrame(bmp, pal, DitheringMethod.FloydSteinberg); } } |
For more information about working with image colors using DsImaging, see DsImaging sample browser.