[GDI+]由彩色到黑白

[GDI+]由彩色到黑白
                                           由彩色到黑白

彩色图转换为灰度图公式很简单:
Y=0.3RED+0.59GREEN+0.11 Blue

用GDI+实现的方式由两种:
1. 直接用上述公式修改象素点
2. 用ColorMatrix。

下面是用ColorMatrix实现示例:

using   namespace  Gdiplus;
    Image img(wszFileName);
    Graphics graphics(GetDC()
-> GetSafeHdc());
    ColorMatrix cm
=   {0.3f0.3f0.3f00,
        
0.59f,0.59f,0.59f,00,
        
0.11f,0.11f,0.11f,00,
        
0,    0,    0,    10,
        
0,    0,    0,    01}
;
    ImageAttributes ia;
    ia.SetColorMatrix(
& cm);

    
float  x  =  ( float )img.GetWidth();
    
float  y  =  ( float )img.GetHeight();
    graphics.DrawImage(
& img, 
        RectF(
0.0f , 0.0f ,x,y,
        
0.0f , 0.0f ,x,y, 
        UnitPixel,
        
& ia);

效果图:
  CToG_001.JPG
程序下载

你可能感兴趣的:([GDI+]由彩色到黑白)