将RGB图像转换为灰度

时间:2014-02-03 12:31:26

标签: c# rgb grayscale

我是编程新手。我是一名大学生。是否可以使用visual studio将RGB图像转换为灰度,使用C#?

有一个特定的文件夹,其中包含RGB jpeg图像,并且每天都有新的jpg文件。我需要制作一个exe文件,将它们转换为灰度。

我是否必须为此工作安装新库,或者VS2013的标准库是否足够?

1 个答案:

答案 0 :(得分:1)

标准库就足够了。

我曾经使用过这段代码:

public static Bitmap GrauwertBild(Bitmap input) 
{
  Bitmap greyscale = new Bitmap(input.Width, input.Height);
  for (int x = 0; x < input.Width; x++)
  {
    for (int y = 0; y < input.Height; y++)
    {
     Color pixelColor = input.GetPixel(x, y);
     //  0.3 · r + 0.59 · g + 0.11 · b
     int grey = (int)(pixelColor.R * 0.3 + pixelColor.G * 0.59 + pixelColor.B * 0.11);
     greyscale.SetPixel(x, y, Color.FromArgb(pixelColor.A, grey , grey , grey ));
    }
  }
  return greyscale;
}
相关问题