Compartir a través de


How to: Translate Image Colors

A translation adds a value to one or more of the four color components. The color matrix entries that represent translations are given in the following table.

Component to be translated Matrix entry

Red

[4][0]

Green

[4][1]

Blue

[4][2]

Alpha

[4][3]

Example

The following example constructs an Image object from the file ColorBars.bmp. Then the code adds 0.75 to the red component of each pixel in the image. The original image is drawn alongside the transformed image.

The following illustration shows the original image on the left and the transformed image on the right.

Translate Colors

The following table lists the color vectors for the four bars before and after the red translation. Note that because the maximum value for a color component is 1, the red component in the second row does not change. (Similarly, the minimum value for a color component is 0.)


Original Translated

Black (0, 0, 0, 1)

(0.75, 0, 0, 1)

Red (1, 0, 0, 1)

(1, 0, 0, 1)

Green (0, 1, 0, 1)

(0.75, 1, 0, 1)

Blue (0, 0, 1, 1)

(0.75, 0, 1, 1)

Dim image As New Bitmap("ColorBars.bmp")
Dim imageAttributes As New ImageAttributes()
Dim width As Integer = image.Width
Dim height As Integer = image.Height

Dim colorMatrixElements As Single()() = { _
   New Single() {1, 0, 0, 0, 0}, _
   New Single() {0, 1, 0, 0, 0}, _
   New Single() {0, 0, 1, 0, 0}, _
   New Single() {0, 0, 0, 1, 0}, _
   New Single() {0.75F, 0, 0, 0, 1}}

Dim colorMatrix As New ColorMatrix(colorMatrixElements)

imageAttributes.SetColorMatrix( _
   colorMatrix, _
   ColorMatrixFlag.Default, _
   ColorAdjustType.Bitmap)

e.Graphics.DrawImage(image, 10, 10, width, height)

' Pass in the destination rectangle (2nd argument), the upper-left corner 
' (3rd and 4th arguments), width (5th argument),  and height (6th 
' argument) of the source rectangle.
e.Graphics.DrawImage( _
   image, _
   New Rectangle(150, 10, width, height), _
   0, 0, _
   width, _
   height, _
   GraphicsUnit.Pixel, _
   imageAttributes)
Image image = new Bitmap("ColorBars.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;

float[][] colorMatrixElements = { 
   new float[] {1,  0,  0,  0, 0},
   new float[] {0,  1,  0,  0, 0},
   new float[] {0,  0,  1,  0, 0},
   new float[] {0,  0,  0,  1, 0},
   new float[] {.75f, 0, 0, 0, 1}};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(
   colorMatrix,
   ColorMatrixFlag.Default,
   ColorAdjustType.Bitmap);

e.Graphics.DrawImage(image, 10, 10, width, height);

e.Graphics.DrawImage(
   image,
   new Rectangle(150, 10, width, height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle 
   width,       // width of source rectangle
   height,      // height of source rectangle
   GraphicsUnit.Pixel,
   imageAttributes);

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. Replace ColorBars.bmp with an image file name and path that are valid on your system.

See Also

Reference

ColorMatrix
ImageAttributes

Other Resources

Graphics and Drawing in Windows Forms
Recoloring Images