一、颜色变换ColorMatrix
ColorMatrix位于System.Drawing.Imaging命名空间。 它的构造函数有两个, public ColorMatrix(); public ColorMatrix(Single[][]); 如果使用参数为空的构造函数,可以在程序指定颜色矩阵的各值,也可以直接在构造函数中传递矩阵各值,这样采用第二个构造函数。 虽然在SDK文档中解释ARGB 矢量表示为 Alpha、Red(红色)、Green(绿色)、Blue(蓝色)和 W,其实排列顺序应该是RGBAW,Alpha是第四个向量,如[R G B A W]。 在颜色矩阵中,第5行5列的值必须是1,,在5列的各值必须是0(除第5行5列),这样保证最后得到的颜色矢量的w向量的值仍然为1.微软为什么要加以个w向量呢,下面慢慢解释

颜色变换公式: [r g b a w]=[r0 g0 b0 a0 w0]M
可以得到 r=A00r0 + A10g0+ A20b0 + A30a0 + A40w0 g=A01r0 + A11g0+ A21b0 + A31a0 + A41w0 b=A02r0 + A12g0+ A22b0 + A32a0 + A42w0 a=A03r0 + A13g0+ A23b0 + A33a0 + A43w0 w=A04r0 + A14g0+ A24b0 + A34a0 + A44w0 因为w0=1,A04=A14=A24=A34=0,A44=1 所以w总为1 大家可以看到新的r,g,b,a向量除了和原来颜色的值r,g,b,a相关外,还可以增加一 与原来颜色无关的值,分别为A40w0、A41w0、A42w0、A43w0,这就是为什么加了一向量w。 构造不同的矩阵,注意A40、A41、A42、A43的值一般取值在[-1,1],否则一旦超出值,很容易达到颜色的最大值。 如
显示出来是全白色 你可以察看我构造的几个颜色矩阵进行的图像处理的效果
Red通道
 
Green通道
 
Blue通道
 
Alpha通道
 
灰度
 
主要代码:
Bitmap bitmap = new Bitmap("smallnest.bmp");
// Initialize the color matrix. float[][] matrixItems ={ new float[] {0.4f, 0.2f, 0.4f, 0, 0}, new float[] {0.4f, 0.2f, 0.4f, 0, 0}, new float[] {0.4f, 0.2f, 0.4f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1}}; ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
// Create an ImageAttributes object and set its color matrix. ImageAttributes imageAtt = new ImageAttributes(); imageAtt.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
// Now draw the semitransparent bitmap image. int iWidth = bitmap.Width; int iHeight = bitmap.Height; this.Width=iWidth; this.Height=iHeight; e.Graphics.DrawImage( bitmap, new Rectangle(0, 0, iWidth, iHeight), // destination rectangle 0.0f, // source rectangle x 0.0f, // source rectangle y iWidth, // source rectangle width iHeight, // source rectangle height GraphicsUnit.Pixel, imageAtt); 
|