ColorMatrix 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
定义包含 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) 的图像并应用前面段落中描述的转换。
下图左侧显示原始图像,右侧显示转换后的图像。
以下示例中的代码使用以下步骤执行重新着色:
初始化一个 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(零)行第 1 列的元素。 |
Matrix02 |
获取或设置此 ColorMatrix 第 0(零)行第 2 列的元素。 |
Matrix03 |
获取或设置此 ColorMatrix 第 0(零)行第 3 列的元素。 表示 alpha 组件。 |
Matrix04 |
获取或设置此 ColorMatrix 第 0(零)行第 4 列的元素。 |
Matrix10 |
获取或设置此 ColorMatrix 第 1 行第 0(零)列的元素。 |
Matrix11 |
获取或设置此 ColorMatrix 第 1 行第 1 列的元素。 |
Matrix12 |
获取或设置此 ColorMatrix 第 1 行第 2 列的元素。 |
Matrix13 |
获取或设置此 ColorMatrix 第 1 行第 3 列的元素。 表示 alpha 组件。 |
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 列的元素。 表示 alpha 组件。 |
Matrix34 |
获取或设置此 ColorMatrix 第 3 行第 4 列的元素。 |
Matrix40 |
获取或设置此 ColorMatrix 第 4 行第 0(零)列的元素。 |
Matrix41 |
获取或设置此 ColorMatrix 第 4 行第 1 列的元素。 |
Matrix42 |
获取或设置此 ColorMatrix 第 4 行第 2 列的元素。 |
Matrix43 |
获取或设置此 ColorMatrix 第 4 行第 3 列的元素。 表示 alpha 组件。 |
Matrix44 |
获取或设置此 ColorMatrix 第 4 行第 4 列的元素。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈