ColorMatrix 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义包含 RGBAW 空间坐标的 5 x 5 矩阵。 ImageAttributes 类的几种方法使用颜色矩阵调整图像颜色。 无法继承此类。
public ref class ColorMatrix sealedpublic sealed class ColorMatrixtype ColorMatrix = classPublic NotInheritable Class ColorMatrix- 继承
- 
				ColorMatrix
示例
以下示例采用所有一种颜色(0.2、0.0、0.4、1.0)的图像,并应用上述段落中所述的转换。
下图显示了左侧的原始图像和右侧的转换图像。
               
              
            
以下示例中的代码使用以下步骤执行重新着色:
- 初始化 ColorMatrix 对象。 
- 创建一个 ImageAttributes 对象,并将 ColorMatrix 对象传递给 ImageAttributes 对象的 SetColorMatrix 方法。 
- 将 ImageAttributes 对象传递给 Graphics 对象的 DrawImage 方法。 
前面的示例设计用于 Windows 窗体,它需要 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)开始,并应用以下转换:
- 将红色分量加倍 
- 将 0.2 添加到红色、绿色和蓝色组件 
以下矩阵乘法将按列出的顺序执行转换对。
               
              
            
颜色矩阵的元素按行和列编制索引(从零开始)。 例如,矩阵 M 的第五行和第三列中的条目由 M[4][2] 表示。
5×5 个标识矩阵(如下图所示)在对角线上具有 1 个,其他位置有 0 个。 如果将颜色矢量乘以标识矩阵,则颜色矢量不会更改。 形成颜色转换矩阵的一种便捷方法是从标识矩阵开始,并进行生成所需转换的小更改。
               
              
            
有关矩阵和转换的更详细讨论,请参阅 坐标系和转换。
构造函数
| ColorMatrix() | 初始化 ColorMatrix 类的新实例。 | 
| ColorMatrix(ReadOnlySpan<Single>) | 定义包含 RGBAW 空间坐标的 5 x 5 矩阵。 ImageAttributes 类的几种方法使用颜色矩阵调整图像颜色。 无法继承此类。 | 
| ColorMatrix(Single[][]) | 使用指定矩阵  | 
属性
| 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) |