Color Transformation Applications in C# GDI+ Programming
(Page 1 of 4 )
In this last installment of the series related to color transformation in GDI+, we will delve into two typical types of color transformation applications: one is color remapping and the other is color channel isolating.
Now, let’s start with what color remapping is and how we can use it in real scenarios.
Color Remapping
As far as color mapping is concerned, the commonly-used color palette under the GDI environment may come to mind. By adjusting the standard colors via the color palette, we can redefine the values of the color indexes in the color palette. A typical application is the fading in and out effects with an image via the color palette technique. However, the color palette can only be utilized when the number of the colors is less than 256. When the number of the colors on the screen is greater than 256, the color palette technique can no longer take effect. GDI+ introduces a new form of ‘color remapping’ that redefines the specified colors. It is mainly based on the idea of the color palette,
In GDI+, the colors of an image can be adjusted when they are rendered according to the facts of the requests. The key to achieving this result is to redefine the colors, i.e. color remapping. In fact, color remapping is the process of converting the colors in an image according to a color remap table, which, in fact, is an array of ColorMap structures. Each ColorMap structure in the array has an oldColor member and a newColor member.
Let’s consider the following typical scenario. During the course of making a television program, the background color is generally set blue. In this case, when the background needs to be changed or the videos need to be synthesized, the blue color in the background can be weeded out so as to maintain the main picture. Based on this, let’s consider how to weed out the background color of an image.
The commonly-used solution to solve the above problem is to replace the background color with a transparent color. In fact, GDI+ has introduced a class named ColorMap to perform this function, where some kind of color redirection relation is defined. Furthermore, we can substitute any of the other colors in the image using ColorMap.
When GDI+ draws an image, each pixel of the image is compared to the array of old colors. If a pixel's color matches an old color, its color is changed to the corresponding new color. The colors are changed only for rendering—the color values of the image itself (stored in an Image or Bitmap object) are not changed.
To draw a remapped image, initialize an array of ColorMap structures. Pass the address of that array to the SetRemapTable method of an ImageAttributes object, and then pass the address of the ImageAttributes object to the Graphics::DrawImage method of a Graphics object.
Let’s examine several signatures of the SetRemapTable method in detail.
public void SetRemapTable (ColorMap[] map);
public void SetRemapTable ( ColorMap[] map,ColorAdjustType type);
For easy use, let’s also list the definition of the ColorMap class disassembled using Luts Roeder’s .NET Reflector:
public sealed class ColorMap
{
// Fields
private Color newColor = new Color();
private Color oldColor = new Color();
// Properties
public Color NewColor
{
get
{
return this.newColor;
}
set
{
this.newColor = value;
}
}
public Color OldColor
{
get
{
return this.oldColor;
}
set
{
this.oldColor = value;
}
}
}
Now let's construct a sample application that pulls the foreground Image object out of the original image named Nemo_Blue.bmp.
Next: Pulling Out the Foreground Image Demo >>
More C# Articles
More By Xianzhong Zhu