語言

ColorMatrix 類別

定義

定義包含 RGBAW 空間座標的 5 x 5 矩陣。 本 ImageAttributes 類別的幾種方法透過使用色彩矩陣來調整影像顏色。 此類別無法獲得繼承。

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),並套用上述段落中所述的轉換。

下圖顯示左側為原始影像,右側為轉換後的影像。

顏色

下列範例中的程式代碼會使用下列步驟來執行重新著色:

  1. 初始化一個 ColorMatrix 物件。

  2. 建立一個 ImageAttributes 物件,然後把物件 ColorMatrix 傳給 SetColorMatrix 該 ImageAttributes 物件的方法。

  3. 將物件傳遞 ImageAttributes 給 DrawImage 物件 Graphics 的方法。

前述範例是為 Windows Forms 設計,且需要 PaintEventArgse,這是事件處理器的 Paint 參數

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 同質值。 例如,ARGB 向量會以紅色、綠色、藍色、Alpha 和 w 表示,其中 w 一律為 1。

例如,假設您想要從色彩 (0.2、 0.0、 0.4、 1.0) 開始,並套用下列轉換:

  1. 將紅色元件加倍

  2. 將0.2新增至紅色、綠色和藍色元件

下列矩陣乘法會依照列出的順序執行轉換組。

重新上色

色彩矩陣的專案會依數據列和數據行編製索引(以零起始)。 例如,矩陣 M 第五列和第三欄的專案是以 M[4][2] 表示。

5×5 身分識別矩陣(如下圖所示)在對角線上有 1 個,其他地方有 0 個。 如果您將色彩向量乘以識別矩陣,則色彩向量不會變更。 形成色彩轉換矩陣的便利方式,就是從識別矩陣開始,並進行產生所需轉換的小型變更。

重新上色

欲了解更多矩陣與轉換的詳細討論,請參閱 座標系與轉換。

建構函式

名稱 Description
ColorMatrix()

初始化 ColorMatrix 類別的新執行個體。

ColorMatrix(ReadOnlySpan<Single>)

初始化一個包含指定矩陣元素的新類別實例 ColorMatrix 。

ColorMatrix(Single[][])

使用指定矩陣ColorMatrix中的元素初始化該newColorMatrix類別的新實例。

屬性

名稱 Description
Item[Int32, Int32]

取得或設定元素於指定的列與欄。ColorMatrix

Matrix00

取得或將元素設於此 ColorMatrix0(0)列與 0 欄。

Matrix01

取得或設定元素位於此 ColorMatrix0 (0) 列及第一欄。

Matrix02

取得或將元素設於此 ColorMatrix的 0(0)列及第二欄。

Matrix03

取得或將元素設於此 ColorMatrix的 0(0)列及第三欄。 表示Alpha元件。

Matrix04

取得或設定元素位於此 ColorMatrix0 (0) 行及第四欄。

Matrix10

取得或設定元素位於該欄位 ColorMatrix的第一列和0(零)欄。

Matrix11

取得或設定元素位於此 ColorMatrix的第一列第一欄。

Matrix12

取得或設定元素位於此 ColorMatrix的第一列第二欄。

Matrix13

取得或設定元素位於此 ColorMatrix的第一列第三欄。 表示Alpha元件。

Matrix14

取得或設定元素位於此 ColorMatrix的第一列第四欄。

Matrix20

取得或將元素設於此列 ColorMatrix的第二列與0列。

Matrix21

取得或設定元素位於此 ColorMatrix的第二列第一欄。

Matrix22

取得或將元素設於此 ColorMatrix的第二列第二欄。

Matrix23

取得或將元素設於此序列 ColorMatrix的第二列第三欄。

Matrix24

取得或將元素設於此序列 ColorMatrix的第二列第四欄。

Matrix30

取得或將元素設於此欄位 ColorMatrix的第三列與0(0)欄。

Matrix31

取得或設定元素位於此序列 ColorMatrix的第三列第一欄。

Matrix32

取得或設定元素位於此序列 ColorMatrix的第三列第二欄。

Matrix33

取得或設定元素位於此 ColorMatrix的第三列第三欄。 表示Alpha元件。

Matrix34

取得或將元素設於此 ColorMatrix的第三行第四欄。

Matrix40

取得或將元素設於該欄位 ColorMatrix的第四列與0(零)欄。

Matrix41

取得或設定元素位於此 ColorMatrix的第四列第一欄。

Matrix42

取得或將元素設於此 ColorMatrix的第四列第二欄。

Matrix43

取得或設定元素位於此序列 ColorMatrix的第四列第三欄。 表示Alpha元件。

Matrix44

取得或將元素設於此 ColorMatrix的第四列第四欄。

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱