更新:2007 年 11 月
GDI+ 提供了用于存储和操作图像的 Image 和 Bitmap 类。Image 和 Bitmap 对象按照 32 位数存储每个像素的颜色:红、绿、蓝和 Alpha 各 8 位。这四个分量的值都是 0 到 255,其中 0 表示没有亮度,255 表示最大亮度。alpha 分量指定颜色的透明度:0 表示完全透明,255 表示完全不透明。
颜色矢量采用 4 元组形式(红色、绿色、蓝色、alpha)。例如,颜色矢量 (0, 255, 0, 255) 表示一种没有红色和蓝色但绿色达到最大亮度的不透明颜色。
表示颜色的另一种惯例是用数字 1 表示亮度达到最大。使用这种约定,上一段中描述的颜色将可以由矢量 (0, 1, 0, 1) 表示。在执行颜色变换时,GDI+ 约定 1 作为最大亮度。
可通过用 4×4 矩阵乘以这些颜色矢量将线性变换(旋转和缩放等)应用到颜色矢量中。但是,您不能使用 4×4 矩阵进行平移(非线性)。如果在每个颜色矢量中再添加一个虚拟的第 5 坐标(例如,数字 1),则可使用 5×5 矩阵应用任何组合形式的线性变换和平移。由线性变换组成的后跟平移的变换称为仿射变换。
例如,假设您希望从颜色 (0.2, 0.0, 0.4, 1.0) 开始并应用下面的变换:
- 将红色分量乘以 2。 
- 将 0.2 添加到红色、绿色和蓝色分量中。 
下面的矩阵乘法将按照列出的顺序进行这对变换。
.gif)
颜色矩阵的元素按照先行后列(从 0 开始)的顺序进行索引。例如,矩阵 M 的第五行第三列由 M[4][2] 表示。
5×5 单位矩阵(在下面的插图中显示)在对角线上为 1,在其他任何地方为 0。如果用单位矩阵乘以颜色矢量,则颜色矢量不会发生改变。形成颜色变换矩阵的一种简便方法是从单位矩阵开始,然后进行较小的改动以产生所需的变换。
.gif)
有关矩阵和变换的更详细的讨论,请参见坐标系统和变形。
示例
下面的示例采用一个使用一种颜色 (0.2, 0.0, 0.4, 1.0) 的图像,并应用上一段中描述的变换。
下面的插图在左侧显示原来的图像,在右侧显示变换后的图像。
.png)
下面示例中的代码使用以下步骤进行重新着色:
- 初始化 ColorMatrix 对象。 
- 创建一个 ImageAttributes 对象,并将 ColorMatrix 对象传递给 ImageAttributes 对象的 SetColorMatrix 方法。 
- 将 ImageAttributes 对象传递给 Graphics 对象的 DrawImage 方法。 
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)
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);
编译代码
前面的示例是为使用 Windows 窗体而设计的,它需要 Paint 事件处理程序的参数 PaintEventArgs e。