How to transfer color images to grey images using C#

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.




Answer this question

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

    Thanks a lot for you guys' suggestions.

  • 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

    And yet one more greyscale matrix; this one happens to be the one .NET uses to convert a colored object to the 'disabled' color:

    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

    Thanks a lot for your so quick response. The performance is very key for this project. Will "change image attribute to change to Grayscale pictures" be fast Any other ideas or other sample codes Thanks a lot again.

  • 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();



  • How to transfer color images to grey images using C#