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 Create 物件,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 個。 如果您將色彩向量乘以識別矩陣,則色彩向量不會變更。 形成色彩轉換矩陣的便利方式,是從身分識別矩陣開始,並進行產生所需轉換的小型變更。

重新著色重新著色

如需矩陣和轉換的詳細討論,請參閱 座標系統和轉換

建構函式

ColorMatrix()

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

ColorMatrix(ReadOnlySpan<Single>)

定義含有 RGBAW 空間座標的 5 x 5 矩陣。 ImageAttributes 類別的多個方法會使用色彩矩陣來調整影像色彩。 此類別無法獲得繼承。

ColorMatrix(Single[][])

使用在指定的矩陣 newColorMatrix 中的項目,初始化 ColorMatrix 類別的新執行個體。

屬性

Item[Int32, Int32]

取得或設定在 ColorMatrix 中指定列和行的項目。

Matrix00

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第 0 行的項目。

Matrix01

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第一行的項目。

Matrix02

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第二行的項目。

Matrix03

取得或設定在這個 ColorMatrix 中第 0 (零) 列和第三行的項目。 表示 Alpha 元件。

Matrix04

取得或設定在這個 ColorMatrix 中第 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 (零) 行的項目。

Matrix31

取得或設定在這個 ColorMatrix 中第三列和第一行的項目。

Matrix32

取得或設定在這個 ColorMatrix 中第三列和第二行的項目。

Matrix33

取得或設定在這個 ColorMatrix 中第三列和第三行的項目。 表示 Alpha 元件。

Matrix34

取得或設定在這個 ColorMatrix 中第三列和第四行的項目。

Matrix40

取得或設定在這個 ColorMatrix 中第四列和第 0 (零) 行的項目。

Matrix41

取得或設定在這個 ColorMatrix 中第四列和第一行的項目。

Matrix42

取得或設定在這個 ColorMatrix 中第四列和第二行的項目。

Matrix43

取得或設定在這個 ColorMatrix 中第四列和第三行的項目。 表示 Alpha 元件。

Matrix44

取得或設定在這個 ColorMatrix 中第四列和第四行的項目。

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

適用於

另請參閱