Color Transformation Applications in C# GDI+ Programming - About the Color Channel
(Page 3 of 4 )
Quite a few readers may be familiar with Adobe’s PhotoShop, the famous two dimensional picture and image processing software. One thing PhotoShop supports is color channel isolating. By decomposing the original compound image into three isolated r/g/b channels, we can edit the current image more easily. With all this work done, we can compose the three channels again.
In fact, color mapping is a kind of color filtering. If we use the color transformation matrices to set the related color saturation to zero, then this color will be invisible when outputed.
Using the tools in GDI+, we can use the SetOutputChannel method of the ImageAttributes class to output the individual color channels. But, regrettably, the SetOutputChannel method only supports outputting the C/M/Y/K channels. In fact, with the help of different color transformation matrices, we can also succeed in outputting the individual R/G/B channels. Next, we will see an example that accomplishes this using the color transformation matrix technique.
Isolating R/G/B Channel Demo
As usual, let’s visualize the finished line. Figure 2 demonstrates the related running-time snapshot.
Figure 2—using the color transformation matrix to isolate the R/G/B channel
_html_m72216cb7.png)
In Figure 2, the original compound image is rendered at the upper left corner, with the other three being the images maintaining the R/G/B channels.
Now, let’s dig into the "how-to's" of the above sample. The following lists the souce code with detailed explanations.
Graphics graphics = this.CreateGraphics();
graphics.Clear(Color.White);
//load the image
Bitmap image = new Bitmap("jieba.bmp");
//render the source image
graphics.DrawImage(image, 50, 50);
//get the necessary data required by the later rendering operation
int width = image.Width;
int height = image.Height;
ImageAttributes imageAttributes = new ImageAttributes();
//set up the red channel
float[][] colorMatrixElements =
{
new float[]{1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
//enable the color transformation matrix
imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//use the above color transformation matrix to render the new picture
//corresponding to the one in the red channel
graphics.TranslateTransform(width + 60, 50);
graphics.DrawImage(image, new Rectangle(0, 0, width, height),
0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);
//clear out the already-used color transformation
imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);
//set up the second channel—the green channel
float[][] colorMatrixElements2 =
{
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix2 = new ColorMatrix(colorMatrixElements2);
//enable the color transformation matrix
imageAttributes.SetColorMatrix(
colorMatrix2,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//use the color transformation matrix to render the new picture
//corresponding to the one in the green channel
//first reset transformation matrix to identity
graphics.ResetTransform();
graphics.TranslateTransform(50, height + 60);
graphics.DrawImage(image, new Rectangle(0, 0, width, height),
0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);
//clear out the used color transformation
imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);
//set up the blue channel
float[][] colorMatrixElements3 =
{
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix3 = new ColorMatrix(colorMatrixElements3);
//enable the color transformation matrix
imageAttributes.SetColorMatrix(
colorMatrix3,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//use the color transformation matrix to output picture
graphics.TranslateTransform(width+10 , 0);
graphics.DrawImage(image, new Rectangle(0, 0, width, height),
0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);
By using the color transformation matrices we’ve easily taken out the independent R/G/B channels. This resembles the function that PhotoShop has provided. However, the above sample is the simplest application of color transformation. You can easily find out that if you modify the R/G/B/A component at the main diagonal in the color transformation matrix, you can obtain some special and even miraculous effects.
To further grasp the above idea, I recommend that you test the SetOutputChannel method of the ImageAttributes class so you can output the C/M/Y/K channels in order to make a good comparison with the idea supplied in this article.
Next: Summary >>
More C# Articles
More By Xianzhong Zhu