## CSDN博客

### GDI+中的矩阵变换

ColorMatrix位于System.Drawing.Imaging命名空间。

public ColorMatrix();
public ColorMatrix(Single[][]);

[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

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,

// 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);

0 0