Performing Color Transformation Operations in C# GDI+ - Scaling Color Operation
(Page 2 of 4 )
A scaling transformation corresponds to the matrix multiplication. During the scaling operation, each of the four color components of a color is multiplied by a specified number. The following Figure 4 gives the associated transformation matrix.
Figure 4-the color matrix entries that represent scaling transformation
_html_46220fb5.png)
It's worth noticing that the scaling transformation will not influence the color saturation.
Now, let's also construct a sample to see how the scaling transformation will affect the changing colors. The following example constructs two Image objects from the original head.bmp file. Figure 5 gives the related running-time snapshot of the sample.
Figure 5-the running-time snapshot of the scaling transformation sample
_html_528775a8.png)
Next, let's do some research into the source code, as listed below.
Graphics graphics=this.CreateGraphics();
graphics.Clear(Color.White);
//load the original image
Bitmap image = new Bitmap("head.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
//define the first color transformation matrix
float[][] colorMatrixElements=
{
new float[]{0.5f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.5f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 0.5f, 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);
//define the second color transformation matrix
float[][] colorMatrixElements2=
{
new float[]{0.5f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.5f, 0.0f, 0.0f, 0.0f},
new float[]{0.0f, 0.0f, 500.50f, 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 first 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);
//clear up the former color transformation
imageAttributes.ClearColorMatrix(ColorAdjustType.Bitmap);
//reload another color transformation matrix colorMatrix2
imageAttributes.SetColorMatrix(
colorMatrix2,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
//use matrix colorMatrix2 to adjust the color and render the transformed image
graphics.TranslateTransform(width+10,0);
graphics.DrawImage(
image,
new Rectangle(0, 0, width, height),
0, 0,
width, height,GraphicsUnit.Pixel,
imageAttributes);
Apparently, here we have leveraged two color matrices to perform the scaling operation. The first three elements in the main diagonal in the first matrix are all 0.5, which indicates the red, green, and blue components of each pixel in the image are evenly scaled down 50 percent, which results in the bright colors in the image being turned into gray. Take the red component for example. The color matrix transformation with the red component is achieved through the following matrix computation in Figure 6.
Figure 6-the red component related matrix transformation
_html_7055cc9c.png)
The matrix transformation above indicates that the red color is changed into gray (127, 0, 0, 0) after the transformation.
Moreover, we have defined the second transformation matrix, colorMatrix2. In contrast to the first matrix, the third element in the main diagonal in this matrix becomes 500.5, which means that the blue component is multiplied by 500.5 during the transformation. What on earth will this result in?
As you may imagine, the third picture in Figure 5 is the output result through the colorMatrix2 matrix's associated transformation. Note that in the color matrix calculation of GDI+ when the result of a color component is greater than 1, the factual saturation of the special color only utilizes the fractional part. For example, 500.5 x 1=500.5, and the fractional part of 500.5 is 0.5. Retaining only the fractional part ensures that the result is always in the interval [0, 1].
Next, let's move to another color transformation-rotating color operation.
Next: Rotating Color Operation >>
More C# Articles
More By Xianzhong Zhu