Document Solutions for Imaging
Features / Work with Image Colors
In This Topic
    Work with Image Colors
    In This Topic

    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.

    Adjust Color Intensity 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
    Image of a house before applying autolevel Image of a house after applying autolevel

    To improve the colors of an image: 

    1. Initialize a new instance of GcBitmap class and load the image in it.
    2. Invoke the AutoLevel method of GcBitmap class.
    3. Save the image with adjusted contrast.
      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");
      }
      

    Back to Top

    Adjust Image Histogram Levels 

    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
    Image of a house before adjusting histogram levels Image of a house after adjusting histogram levels

    To adjust levels of an image histogram:

    1. Initialize a new instance of GcBitmap class and load the image in it.
    2. Invoke the AdjustLevels method.
    3. Save the image with adjusted histogram in the desired format.
      C#
      Copy Code
      public void SetBrightness()
      {
          GcBitmap bmp = new GcBitmap("Images/house.jpg");
          bmp.AdjustLevels(0, 0x00646464, 0x00969696, 0x00FAFAFA);
          bmp.SaveAsJpeg("brighthouse.jpg");
      }
      

    Back to Top

    Work with Color Channels

    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
    Image of a beautiful house Image of the house after exporting blue color channel Image of the house after exporting green color channel

     

    To export Blue and Green color channels of a colored image:

    1. Create an instance of GcBitmap class and load a colored image in it.
    2. To create a grayscale image for one of the color channel of a colored image, either invoke ToGrayscaleBitmap method or ExportColorChannel method of GcBitmap class and pass the color channel as a parameter.
    3. Save the image using the SaveAsJpeg method.        
      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
    Image of a beautiful house Image of the house based on its red color channel

     

    To create an image based on its Red color channel:

    1. Create an instance of GcBitmap class and load a colored image in it.
    2. Invoke the ToGrayscaleBitmap method of GcBitmap class to create a grayscale image based on Red color channel of the colored image.
    3. Clear the GcBitmap object representing the colored image by invoking the Clear method of GcBitmap class.
    4. Invoke the ImportColorChannel method of GcBitmap class to copy the red color channel data from the GrayScaleBitmap to colored image’s bitmap.
    5. Save the image using the SaveAsJpeg method.
      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);
      }
      

    Back to Top

     

    Work with Color Quantization

    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:

    1. Load an image by instantiating the GcBitmap class.
    2. Generate the Octree color palette using the GenerateOctreePalette method of GcBitmap class.
    3. Create a new GIF image using the AppendFrame method of GcGifWriter class which accepts the octree palette as a parameter.
      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);
          }
      }
      

    Back to Top

    For more information about working with image colors using DsImaging, see DsImaging sample browser.