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. 创建 ImageAttributes 对象并将 ColorMatrix 对象传递给 ImageAttributes 对象的 SetColorMatrix 方法。Create an ImageAttributes object and pass the ColorMatrix object to the SetColorMatrix method of the ImageAttributes object.

  3. ImageAttributes 对象传递到 Graphics 对象的 DrawImage 方法。Pass the ImageAttributes object to the DrawImage method of a Graphics object.

前面的示例旨在与 Windows 窗体一起使用,并且它需要 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)

注解

矩阵系数构成了用于转换 ARGB 同类值的 5 x 5 线性转换。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。2Add 0.2 to the red, green, and blue components

下面的矩阵乘法将按列出的顺序执行转换对。The following matrix multiplication will perform the pair of transformations in the order listed.

重新Recoloring

颜色矩阵的元素按行和 then 列进行索引(从零开始)。The elements of a color matrix are indexed (zero-based) by row and then column. 例如,matrix 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.

重新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[][])

使用指定矩阵 ColorMatrix 中的元素初始化 newColorMatrix 类的新实例。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(零)行第 1 列的元素。Gets or sets the element at the 0 (zero) row and first column of this ColorMatrix.

Matrix02

获取或设置此 ColorMatrix 第 0(零)行第 2 列的元素。Gets or sets the element at the 0 (zero) row and second column of this ColorMatrix.

Matrix03

获取或设置此 ColorMatrix 第 0(零)行第 3 列的元素。Gets or sets the element at the 0 (zero) row and third column of this ColorMatrix. 表示 alpha 组件。Represents the alpha component.

Matrix04

获取或设置此 ColorMatrix 第 0(零)行第 4 列的元素。Gets or sets the element at the 0 (zero) row and fourth column of this ColorMatrix.

Matrix10

获取或设置此 ColorMatrix 第 1 行第 0(零)列的元素。Gets or sets the element at the first row and 0 (zero) column of this ColorMatrix.

Matrix11

获取或设置此 ColorMatrix 第 1 行第 1 列的元素。Gets or sets the element at the first row and first column of this ColorMatrix.

Matrix12

获取或设置此 ColorMatrix 第 1 行第 2 列的元素。Gets or sets the element at the first row and second column of this ColorMatrix.

Matrix13

获取或设置此 ColorMatrix 第 1 行第 3 列的元素。Gets or sets the element at the first row and third column of this ColorMatrix. 表示 alpha 组件。Represents the alpha component.

Matrix14

获取或设置此 ColorMatrix 第 1 行第 4 列的元素。Gets or sets the element at the first row and fourth column of this ColorMatrix.

Matrix20

获取或设置此 ColorMatrix 第 2 行第 0(零)列的元素。Gets or sets the element at the second row and 0 (zero) column of this ColorMatrix.

Matrix21

获取或设置此 ColorMatrix 第 2 行第 1 列的元素。Gets or sets the element at the second row and first column of this ColorMatrix.

Matrix22

获取或设置此 ColorMatrix 第 2 行第 2 列的元素。Gets or sets the element at the second row and second column of this ColorMatrix.

Matrix23

获取或设置此 ColorMatrix 第 2 行第 3 列的元素。Gets or sets the element at the second row and third column of this ColorMatrix.

Matrix24

获取或设置此 ColorMatrix 第 2 行第 4 列的元素。Gets or sets the element at the second row and fourth column of this ColorMatrix.

Matrix30

获取或设置此 ColorMatrix 第 3 行第 0(零)列的元素。Gets or sets the element at the third row and 0 (zero) column of this ColorMatrix.

Matrix31

获取或设置此 ColorMatrix 第 3 行第 1 列的元素。Gets or sets the element at the third row and first column of this ColorMatrix.

Matrix32

获取或设置此 ColorMatrix 第 3 行第 2 列的元素。Gets or sets the element at the third row and second column of this ColorMatrix.

Matrix33

获取或设置此 ColorMatrix 第 3 行第 3 列的元素。Gets or sets the element at the third row and third column of this ColorMatrix. 表示 alpha 组件。Represents the alpha component.

Matrix34

获取或设置此 ColorMatrix 第 3 行第 4 列的元素。Gets or sets the element at the third row and fourth column of this ColorMatrix.

Matrix40

获取或设置此 ColorMatrix 第 4 行第 0(零)列的元素。Gets or sets the element at the fourth row and 0 (zero) column of this ColorMatrix.

Matrix41

获取或设置此 ColorMatrix 第 4 行第 1 列的元素。Gets or sets the element at the fourth row and first column of this ColorMatrix.

Matrix42

获取或设置此 ColorMatrix 第 4 行第 2 列的元素。Gets or sets the element at the fourth row and second column of this ColorMatrix.

Matrix43

获取或设置此 ColorMatrix 第 4 行第 3 列的元素。Gets or sets the element at the fourth row and third column of this ColorMatrix. 表示 alpha 组件。Represents the alpha component.

Matrix44

获取或设置此 ColorMatrix 第 4 行第 4 列的元素。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)

适用于

另请参阅