由彩色到黑白
彩色圖轉(zhuǎn)換為灰度圖公式很簡(jiǎn)單:
Y=0.3RED+0.59GREEN+0.11 Blue
用GDI+實(shí)現(xiàn)的方式由兩種:
1. 直接用上述公式修改象素點(diǎn)
2. 用ColorMatrix。
下面是用ColorMatrix實(shí)現(xiàn)示例:
using namespace Gdiplus;
Image img(wszFileName);
Graphics graphics(GetDC()->GetSafeHdc());

ColorMatrix cm=
{0.3f, 0.3f, 0.3f, 0, 0,
0.59f,0.59f,0.59f,0, 0,
0.11f,0.11f,0.11f,0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1};
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);

效果圖:
程序下載