ColorMatrix クラス

定義

RGBAW 空間の座標を含む 5 x 5 の行列を定義します。 ImageAttributes クラスのいくつかのメソッドは、カラー行列を使用してイメージの色を調整します。 このクラスは継承できません。

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

次の例では、1 つの色 (0.2、0.0、0.4、1.0) の画像を使用し、前の段落で説明した変換を適用しています。

次の図は、左側に元の画像、右側に変換後の画像を示したものです。

色の色

次のコード例では、以下の手順を使って色変更を実行しています。

  1. ColorMatrix オブジェクトを初期化します。

  2. ImageAttributes オブジェクトを作成し、その ImageAttributes オブジェクトの SetColorMatrix メソッドに ColorMatrix オブジェクトを渡します。

  3. ImageAttributes オブジェクトを、Graphics オブジェクトの DrawImage メソッドに渡します。

前の例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。

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)

注釈

行列係数は、ARGB 同種値の変換に使用される 5 x 5 の線形変換を構成します。 たとえば、ARGB ベクトルは赤、緑、青、アルファ、w として表され、w は常に 1 です。

たとえば、(0.2, 0.0, 0.4, 1.0) の色で開始し、次の変換を適用したとします。

  1. 赤の要素を 2 倍にする

  2. 赤、緑、および青の要素に 0.2 を加算する

次の行列の乗算では、変換のペアが列挙順に実行されます。

色を変更する

カラー行列の要素には、行、列の順でインデックス (0 から開始) が付けられます。 たとえば、行列 M の 5 行目の3 列目にあるエントリは、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 (ゼロ) 行および第 1 列にある要素を取得または設定します。

Matrix02

この ColorMatrix の第 0 (ゼロ) 行および第 2 列にある要素を取得または設定します。

Matrix03

この ColorMatrix の第 0 (ゼロ) 行および第 3 列にある要素を取得または設定します。 アルファ要素を表します。

Matrix04

この ColorMatrix の第 0 (ゼロ) 行および第 4 列にある要素を取得または設定します。

Matrix10

この ColorMatrix の第 1 行および第 0 (ゼロ) 列にある要素を取得または設定します。

Matrix11

この ColorMatrix の第 1 行および第 1 列にある要素を取得または設定します。

Matrix12

この ColorMatrix の第 1 行および第 2 列にある要素を取得または設定します。

Matrix13

この ColorMatrix の第 1 行および第 3 列にある要素を取得または設定します。 アルファ要素を表します。

Matrix14

この ColorMatrix の第 1 行および第 4 列にある要素を取得または設定します。

Matrix20

この ColorMatrix の第 2 行および第 0 (ゼロ) 列にある要素を取得または設定します。

Matrix21

この ColorMatrix の第 2 行および第 1 列にある要素を取得または設定します。

Matrix22

この ColorMatrix の第 2 行および第 2 列にある要素を取得または設定します。

Matrix23

この ColorMatrix の第 2 行および第 3 列にある要素を取得または設定します。

Matrix24

この ColorMatrix の第 2 行および第 4 列にある要素を取得または設定します。

Matrix30

この ColorMatrix の第 3 行および第 0 (ゼロ) 列にある要素を取得または設定します。

Matrix31

この ColorMatrix の第 3 行および第 1 列にある要素を取得または設定します。

Matrix32

この ColorMatrix の第 3 行および第 2 列にある要素を取得または設定します。

Matrix33

この ColorMatrix の第 3 行および第 3 列にある要素を取得または設定します。 アルファ要素を表します。

Matrix34

この ColorMatrix の第 3 行および第 4 列にある要素を取得または設定します。

Matrix40

この ColorMatrix の第 4 行および第 0 (ゼロ) 列にある要素を取得または設定します。

Matrix41

この ColorMatrix の第 4 行および第 1 列にある要素を取得または設定します。

Matrix42

この ColorMatrix の第 4 行および第 2 列にある要素を取得または設定します。

Matrix43

この ColorMatrix の第 4 行および第 3 列にある要素を取得または設定します。 アルファ要素を表します。

Matrix44

この ColorMatrix の第 4 行および第 4 列にある要素を取得または設定します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください