C++/GDI+ 学习笔记(四)——实用技巧——颜色矩阵(ColorMatrix)

来源:互联网 发布:买家怎么用淘宝客 编辑:程序博客网 时间:2024/05/16 18:42
         颜色矩阵在GDI+中是十分有用的。
一位颜色信息,由ARGB四部分组成,分别是透明度和三个颜色分量。一个4×4的矩阵即可完整的表示出一个颜色的矩阵,但4*4矩阵无法完成一些变换(例如平移),所以补成了一个5×5的矩阵。最后的一位一直是1。
ColorMatrix colorMatrix = {
           0.3f,     0.3f,     0.3f,     0.0f,     0.0f,
           0.59f,   0.59f,   0.59f,   0.0f,     0.0f,
           0.11f,   0.11f,   0.11f,   0.0f,     0.0f,
           0.0f,     0.0f,     0.0f,     0.3f,     0.0f,
           0.0f,     0.0f,     0.0f,     0.0f,     1.0f };
上面的这个矩阵,是将图片转换成灰阶形式,即去色。[4][4]的位置即是永远为1。[3][3]处,是透明度,这里设置的是30%,所有的数字都是最大为1,即100%。
0.3×Red + 0.59×Green + 0.11×Blue 即是某点颜色的灰阶值。
ColorMatrix colorMatrix = {
           0.3f,     0.3f,     0.3f,     0.0f,     0.0f,
           0.59f,   0.59f,   0.59f,   0.0f,     0.0f,
           0.11f,   0.11f,   0.11f,   0.0f,     0.0f,
           0.0f,     0.0f,     0.0f,     0.3f,     0.0f,
           0.0f,     0.0f,     0.0f,     0.0f,     1.0f };
 
      ImageAttributes imageAtt;
      imageAtt.SetColorMatrix( &colorMatrix );
 
      Bitmap Bmp( “a.bmp” );
      INT iWidth = Bmp.GetWidth();
      INT iHeight = Bmp.GetHeight();
 
      Graphic.DrawImage(  // Graphic是事先初始化好的Graphics
           &Bmp,
           Rect(0, 0, iWidth, iHeight),
           0,                                                               
           0,                                                               
           iWidth,                                             
           iHeight,                                            
           UnitPixel,
           &imageAtt);