ColorMatrix 類別

定義

定義含有 RGBAW 空間座標的 5 x 5 矩陣。Defines a 5 x 5 matrix that contains the coordinates for the RGBAW space. ImageAttributes 類別的多個方法會使用色彩矩陣來調整影像色彩。Several methods of the ImageAttributes class adjust image colors by using a color matrix. 這個類別無法被繼承。This class cannot be inherited.

public ref class ColorMatrix sealed
public sealed class ColorMatrix
type ColorMatrix = class
Public NotInheritable Class ColorMatrix
繼承
ColorMatrix

範例

下列範例會採用全為一色彩 (0.2、0.0、0.4、1.0) 的影像, 並套用先前段落中所述的轉換。The following example takes an image that is all one color (0.2, 0.0, 0.4, 1.0) and applies the transformation described in the preceding paragraphs.

下圖顯示左側的原始影像, 以及右邊的已轉換影像。The following illustration shows the original image on the left and the transformed image on the right.

色彩Colors

下列範例中的程式碼會使用下列步驟來執行重新著色:The code in the following example uses the following steps to perform the recoloring:

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

  2. 建立物件, 並將ColorMatrix物件傳遞ImageAttributesSetColorMatrix物件的方法。 ImageAttributesCreate an ImageAttributes object and pass the ColorMatrix object to the SetColorMatrix method of the ImageAttributes object.

  3. 將物件傳遞給Graphics物件DrawImage的方法。 ImageAttributesPass the ImageAttributes object to the DrawImage method of a Graphics object.

上述範例是針對與 Windows Forms 搭配使用所設計, 而且它PaintEventArgs需要e, 這Paint是事件處理常式的參數。The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler

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,
   ColorAdjustType.Bitmap);

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)

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

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

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

備註

矩陣係數構成 5 x 5 線性轉換, 用於轉換 ARGB 同質值。The matrix coefficients constitute a 5 x 5 linear transformation that is used for transforming ARGB homogeneous values. 例如, ARGB 向量會表示為紅色、綠色、藍色、Alpha 和 w, 其中 w 一律為1。For example, an ARGB vector is represented as red, green, blue, alpha and w, where w is always 1.

例如, 假設您想要以色彩 (0.2、0.0、0.4、1.0) 作為開頭, 並套用下列轉換:For example, suppose you want to start with the color (0.2, 0.0, 0.4, 1.0) and apply the following transformations:

  1. 將紅色元件加倍Double the red component

  2. 將0.2 新增至紅色、綠色和藍色元件Add 0.2 to the red, green, and blue components

下列矩陣乘法會依照列出的循序執行一對轉換。The following matrix multiplication will perform the pair of transformations in the order listed.

重新![著色]重新(~/add/media/recoloring01.gif "著色")Recoloring

色彩矩陣的元素會依資料列和資料行編制索引 (以零為基底)。The elements of a color matrix are indexed (zero-based) by row and then column. 例如, 第五個數據列中的專案和矩陣 M 的第三個數據行是以 M [4] [2] 表示。For example, the entry in the fifth row and third column of matrix M is denoted by M[4][2].

5×5識別矩陣 (如下圖所示) 在對角線和0的任何地方都有1個。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.

重新![著色]重新(~/add/media/recoloring02.gif "著色")Recoloring

如需矩陣和轉換的詳細討論, 請參閱座標系統和轉換For a more detailed discussion of matrices and transformations, see Coordinate Systems and Transformations.

建構函式

ColorMatrix()

初始化 ColorMatrix 類別的新執行個體。Initializes a new instance of the ColorMatrix class.

ColorMatrix(Single[][])

使用在指定的矩陣 newColorMatrix 中的項目,初始化 ColorMatrix 類別的新執行個體。Initializes a new instance of the ColorMatrix class using the elements in the specified matrix newColorMatrix.

屬性

Item[Int32, Int32]

取得或設定在 ColorMatrix 中指定列和行的項目。Gets or sets the element at the specified row and column in the ColorMatrix.

Matrix00

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第 0 行的項目。Gets or sets the element at the 0 (zero) row and 0 column of this ColorMatrix.

Matrix01

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第一行的項目。Gets or sets the element at the 0 (zero) row and first column of this ColorMatrix.

Matrix02

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第二行的項目。Gets or sets the element at the 0 (zero) row and second column of this ColorMatrix.

Matrix03

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第三行的項目。Gets or sets the element at the 0 (zero) row and third column of this ColorMatrix. 表示 Alpha 元件。Represents the alpha component.

Matrix04

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第四行的項目。Gets or sets the element at the 0 (zero) row and fourth column of this ColorMatrix.

Matrix10

取得或設定在這個 ColorMatrix 中第一列和第 0 (零) 行的項目。Gets or sets the element at the first row and 0 (zero) column of this ColorMatrix.

Matrix11

取得或設定在這個 ColorMatrix 中第一列和第一行的項目。Gets or sets the element at the first row and first column of this ColorMatrix.

Matrix12

取得或設定在這個 ColorMatrix 中第一列和第二行的項目。Gets or sets the element at the first row and second column of this ColorMatrix.

Matrix13

取得或設定在這個 ColorMatrix 中第一列和第三行的項目。Gets or sets the element at the first row and third column of this ColorMatrix. 表示 Alpha 元件。Represents the alpha component.

Matrix14

取得或設定在這個 ColorMatrix 中第一列和第四行的項目。Gets or sets the element at the first row and fourth column of this ColorMatrix.

Matrix20

取得或設定在這個 ColorMatrix 中第二列和第 0 (零) 行的項目。Gets or sets the element at the second row and 0 (zero) column of this ColorMatrix.

Matrix21

取得或設定在這個 ColorMatrix 中第二列和第一行的項目。Gets or sets the element at the second row and first column of this ColorMatrix.

Matrix22

取得或設定在這個 ColorMatrix 中第二列和第二行的項目。Gets or sets the element at the second row and second column of this ColorMatrix.

Matrix23

取得或設定在這個 ColorMatrix 中第二列和第三行的項目。Gets or sets the element at the second row and third column of this ColorMatrix.

Matrix24

取得或設定在這個 ColorMatrix 中第二列和第四行的項目。Gets or sets the element at the second row and fourth column of this ColorMatrix.

Matrix30

取得或設定在這個 ColorMatrix 中第三列和第 0 (零) 行的項目。Gets or sets the element at the third row and 0 (zero) column of this ColorMatrix.

Matrix31

取得或設定在這個 ColorMatrix 中第三列和第一行的項目。Gets or sets the element at the third row and first column of this ColorMatrix.

Matrix32

取得或設定在這個 ColorMatrix 中第三列和第二行的項目。Gets or sets the element at the third row and second column of this ColorMatrix.

Matrix33

取得或設定在這個 ColorMatrix 中第三列和第三行的項目。Gets or sets the element at the third row and third column of this ColorMatrix. 表示 Alpha 元件。Represents the alpha component.

Matrix34

取得或設定在這個 ColorMatrix 中第三列和第四行的項目。Gets or sets the element at the third row and fourth column of this ColorMatrix.

Matrix40

取得或設定在這個 ColorMatrix 中第四列和第 0 (零) 行的項目。Gets or sets the element at the fourth row and 0 (zero) column of this ColorMatrix.

Matrix41

取得或設定在這個 ColorMatrix 中第四列和第一行的項目。Gets or sets the element at the fourth row and first column of this ColorMatrix.

Matrix42

取得或設定在這個 ColorMatrix 中第四列和第二行的項目。Gets or sets the element at the fourth row and second column of this ColorMatrix.

Matrix43

取得或設定在這個 ColorMatrix 中第四列和第三行的項目。Gets or sets the element at the fourth row and third column of this ColorMatrix. 表示 Alpha 元件。Represents the alpha component.

Matrix44

取得或設定在這個 ColorMatrix 中第四列和第四行的項目。Gets or sets the element at the fourth row and fourth column of this ColorMatrix.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱