在C#中制作黑白图像

时间:2013-10-01 00:17:25

标签: c# image-processing

我正在尝试在c#中制作黑白图像。这是我的代码:

public static void SetGrayscale(Bitmap b)
    {
        Bitmap temp = (Bitmap) b;
        Bitmap bmap = (Bitmap)temp.Clone();
        Color c;
        for (int i = 0; i < bmap.Width; i++)
        {
            for (int j = 0; j < bmap.Height; j++)
            {
                c = bmap.GetPixel(i, j);
                byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

                bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
            }
        }
        b = (Bitmap)bmap.Clone();
    }       

    static void Main(string[] args)
    {
        Bitmap bm = new Bitmap(Image.FromFile("D:\\users\\visual studio 2010\\Projects\\aaa\\20130924_144411.tif"));
        byte[] pixels = ImageToByte(bm);

        SetGrayscale(bm);
    }

问题是,它不会变成黑白,它仍然是一样的。我没有保存更改的图像吗?这可能是什么问题?

由于

1 个答案:

答案 0 :(得分:1)

你指的是磁盘上的文件没有改变吗?您必须保存灰度位图:bm.Save("D:\\bitmap.tif");

相关问题