# HOW TO：使用色彩矩陣轉換單一色彩How to: Use a Color Matrix to Transform a Single Color

GDI + 提供ImageBitmap來儲存及操作影像的類別。GDI+ provides the Image and Bitmap classes for storing and manipulating images. ImageBitmap物件會儲存每個像素的色彩為 32 位元數字：8 位元用於紅色、 綠色、 藍色和 alpha 的每個。Image and Bitmap objects store the color of each pixel as a 32-bit number: 8 bits each for red, green, blue, and alpha. 每四個元件是從 0 到 255，0 代表不含濃度，表示完整濃度 255 的數字。Each of the four components is a number from 0 through 255, with 0 representing no intensity and 255 representing full intensity. Alpha 元件指定色彩的透明度：0 是完全透明的而且完全不透明 255。The alpha component specifies the transparency of the color: 0 is fully transparent, and 255 is fully opaque.

1. 雙重紅色元件Double the red component

2. 加上紅色、 綠色和藍色元件 0.2Add 0.2 to the red, green, and blue components

5 × 5 身分識別矩陣 （下圖所示） 具有對角線上的 1 和 0 的其他位置。The 5×5 identity matrix (shown in the following illustration) has 1s on the diagonal and 0s everywhere else. 如果您將色彩向量乘以身分識別矩陣時，就不會變更色彩的向量。If you multiply a color vector by the identity matrix, the color vector does not change. 便利的方式來形成 「 色彩 」 轉換的矩陣是以身分識別矩陣開始進行小幅變更會產生所需的轉換。A convenient way to form the matrix of a color transformation is to start with the identity matrix and make a small change that produces the desired transformation.

## 範例Example

1. 初始化ColorMatrix物件。Initialize a ColorMatrix object.

2. 建立ImageAttributes物件，並傳遞ColorMatrix物件SetColorMatrix方法ImageAttributes物件。Create an ImageAttributes object and pass the ColorMatrix object to the SetColorMatrix method of the ImageAttributes object.

3. 傳遞ImageAttributes物件至DrawImage方法Graphics物件。Pass the ImageAttributes object to the DrawImage method of a Graphics object.

Image image = new Bitmap("InputColor.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;

float[][] colorMatrixElements = {
new float[] {2,  0,  0,  0, 0},        // red scaling factor of 2
new float[] {0,  1,  0,  0, 0},        // green scaling factor of 1
new float[] {0,  0,  1,  0, 0},        // blue scaling factor of 1
new float[] {0,  0,  0,  1, 0},        // alpha scaling factor of 1
new float[] {.2f, .2f, .2f, 0, 1}};    // three translations of 0.2

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(
colorMatrix,
ColorMatrixFlag.Default,

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

e.Graphics.DrawImage(
image,
new Rectangle(120, 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);

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

' The following matrix consists of the following transformations:
' red scaling factor of 2
' green scaling factor of 1
' blue scaling factor of 1
' alpha scaling factor of 1
' three translations of 0.2
Dim colorMatrixElements As Single()() = { _
New Single() {2, 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.2F, 0.2F, 0.2F, 0, 1}}

Dim colorMatrix As New ColorMatrix(colorMatrixElements)

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

e.Graphics.DrawImage( _
image, _
New Rectangle(120, 10, width, height), _
0, _
0, _
width, _
height, _
GraphicsUnit.Pixel, _
imageAttributes)