Color oColor,gColor; //定义两个颜色变量,oColor为原始色彩,gColor为对应的灰度色彩 
float brightness;//原始色彩的亮度 
int gRGB;//灰度色彩用RGB来表示,由于R=G=B所以只用一个变量就可以了 
//遍历图像中的每个像素 
for (int i=0; i<oBmp.Width; i++) 

        for (int j=0; j<oBmp.Height; j++) 
        { 
                oColor = oBmp.GetPixel(i,j);//得到像素的原始色彩 
                brightness = oColor.GetBrightness();//得到该色彩的亮度 
                gRGB = (int)(brightness * 255);//用该亮度计算灰度 
                gColor = Color.FromArgb(gRGB,gRGB,gRGB);//组成灰度色彩 
                gBmp.SetPixel(i,j,gColor);最后将该灰度色彩赋予该像素 
        } 
} 代码转贴自http://bbs.tju.edu.cn/

解决方案 »

  1.   

    运用Visual C#完成基本数字图像处理 http://tech.ccidnet.com/pub/article/c1137_a41424_p1.html
      

  2.   

    More is here ...http://www.google.com/search?q=Image+Processing+for+Dummies+with+C%23+and+GDI%2B&ie=UTF-8&hl=zh-CN&btnG=Google%E6%90%9C%E7%B4%A2&lr=
      

  3.   

    public static bool Gray(Bitmap b)
     {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), 
                      ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
    int stride = bmData.Stride;
    System.IntPtr Scan0 = bmData.Scan0;
    unsafe
    {
    byte * p = (byte *)(void *)Scan0;
    int nOffset = stride - b.Width*3;
    byte red, green, blue;
    for(int y=0;y<b.Height;++y)
    {
        for(int x=0; x < b.Width; ++x )
     {
      blue = p[0];
      green = p[1];
      red = p[2];
      p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue);
      p += 3;
     }
     p += nOffset;
    }
    }
    b.UnlockBits(bmData);
    return true;
     }