将一个彩色图像转换成黑白的方法
作者:未知
彩色图像转换为黑白图像时需要计算图像中每像素有效的亮度值,通过匹配像素
亮度值可以轻松转换为黑白图像。
计算像素有效的亮度值可以使用下面的公式:
Y=0.3RED+0.59GREEN+0.11Blue
然后使用 Color.FromArgb(Y,Y,Y) 来把计算后的值转换
转换代码可以使用下面的方法来实现:
[C#]

publicBitmapConvertToGrayscale(Bitmapsource)



{

Bitmapbm=newBitmap(source.Width,source.Height);

for(inty=0;y<bm.Height;y++)



{

for(intx=0;x<bm.Width;x++)



{

Colorc=source.GetPixel(x,y);

intluma=(int)(c.R*0.3+c.G*0.59+c.B*0.11);

bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));

}

}

returnbm;

}
[VB]


PublicFunctionConvertToGrayscale()FunctionConvertToGrayscale(ByValsourceAsBitmap)asBitmap

DimbmasnewBitmap(source.Width,source.Height)

Dimx

Dimy

Fory=0Tobm.Height

Forx=0Tobm.Width

DimcasColor=source.GetPixel(x,y)

DimlumaasInteger=CInt(c.R*0.3+c.G*0.59+c.B*0.11)

bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma)

Next

Next

Returnbm

EndFunction
当然了这是一个好的方法,如果需要更简单的做到图像的色彩转换还可以使用ColorMatrix类