Performing Color Transformation Operations in C# GDI+
(Page 1 of 4 )
With the basic idea of color transformation matrices in mind, in this second installment of a three-part series, we will delve into the four types of operations of color transformation matrices in GDI+, as well as give the related samples.
Translating Color Operation
In reality, a translation is the addition calculation necessary to add a value to one or more of the four color components (i.e. red, green, blue, and alpha). The color transformation matrix entries that represent translations are given in the following Figure 1.
Figure 1-the transformation matrix corresponding to translating color operation
_html_646f9bc6.png)
The components colored gray in Figure 1 represent the elements concerned with translating color operation. Now, let's take a look at an example that performs the translating color operation.
The following example constructs an Image object from the given file named ColorInput.bmp. Then the code adds 0.5 to the red component of each pixel in the image. The transformed image is drawn alongside the original image.
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
//load the original image
Bitmap image = new Bitmap("ColorInput.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
//define a color transformation matrix
float[][] colorMatrixElements=
{
new float[]{1.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, 1.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.0f, 1.0f, 0.0f},
new float[]{0.5f, 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);
//render the source image
graphics.DrawImage(image, 0, 0);
//use the color transformation matrix to render the new target image
graphics.TranslateTransform(width+10,0);
graphics.DrawImage(image, new Rectangle(0, 0, width, height),
0, 0,width, height,GraphicsUnit.Pixel,imageAttributes);
The following Figure 2 shows the related running-time snapshot, with the original image on the left and the transformed one on the right.
Figure 2-the running-time snapshot for the translating color operation
_html_me8c656.png)
From Figure 2, we can easily see that after the translating color transformation, the previous black color is changed into a catsup red color, and the other colors in Figure 2 also change accordingly. Why?
From the above code we can see that the saturation of the red component is set to 0.5 in the color transformation matrix. Take the black color as an example; its related color transformation matrix can be figured out using the following matrix transformation (A=0.5 x 255) in Figure 3.
Figure 3-the color transformation for the black color
_html_m467bae54.png)
In the matrix transformation in Figure 3, the RGB value of black (0, 0, 0) is changed into (128, 0, 0) (i.e. the RGB value of a catsup red color) after the transformation. In the same way, you can figure out the RGBs of all the rest of the colors after the color transformation.
Next, let's shift our attention to another important color calculation-scaling transformation.
Next: Scaling Color Operation >>
More C# Articles
More By Xianzhong Zhu