Hi,
I am trying to use C# 2.0 and visual studio 2005 to compile a window app or web app. In this app, I have to transfer almost 2,000 colorful images to grey images. I do not know how to do that Anyone can provide a block of simple example for that Thanks a lot.

How to transfer color images to grey images using C#
kjehed
Better yet, since human eyes have different sensitivities to R,G,B light, the following matrix gives you are more "natural" transform
float
[][] matrix ={
new float[] {0.299f, 0.299f, 0.299f, 0, 0}, new float[] {0.587f, 0.587f, 0.587f, 0, 0}, new float[] {0.114f, 0.114f, 0.114f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1}};
Darkside
Santhosh Pallikara
Hi, FugersonHall
Use colorMatrix to gray the image can be the efficient way.
Hope it helps.
Thanks
Cory Cundy
Average the R,G,B values of your image
newColor.R = (oldColor.R + oldColor.G + oldColor.B) / 3;
newColor.G = (oldColor.R + oldColor.G + oldColor.B) / 3;
newColor.B = (oldColor.R + oldColor.G + oldColor.B) / 3;
Bitmap bp = new Bitmap("c:\\test.jpg"); for (int i = 0; i < bp.Height; i++) for (int j = 0; j < bp.Width; j++){
Color oldColor = bp.GetPixel(j, i); int newColor = (oldColor.R + oldColor.G + oldColor.B)/3;bp.SetPixel(j, i,
Color.FromArgb(newColor, newColor, newColor));}
However this is a pixel-by-pixel method, which is slow. Maybe you can change image attribute to change to Grayscale pictures....
InfiniZac
float[][] matrix =
{
new float[] {0.2125f, 0.2125f, 0.2125f, 0, 0},
new float[] {0.2577f, 0.2577f, 0.2577f, 0, 0},
new float[] {0.0361f, 00361f, 0.0361f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0.38f, 0.38f, 0.38f, 0, 1}
};
meoryou
GregRoberts
How about this
Image img = new Bitmap("c:\\test.jpg"); ImageAttributes attr = new ImageAttributes(); float[][] matrix ={
new float[] {0.5f, 0.5f, 0.5f, 0, 0}, new float[] {0.5f, 0.5f, 0.5f, 0, 0}, new float[] {0.5f, 0.5f, 0.5f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1}};
ColorMatrix cm = new ColorMatrix(matrix);attr.SetColorMatrix(cm,
ColorMatrixFlag.Default, ColorAdjustType.Bitmap); Graphics g = Graphics.FromImage(img);g.DrawImage(img,
new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attr);img.Save(
"c:\\test2.jpg");g.Dispose();