yrazlik
yrazlik

Reputation: 10777

Making an image black and white in C#

I am trying to make an image black and white in c#. Here is my code:

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);
    }

The problem is, it does not turn into black and white, it is still the same. Am i not saving the changed image? What can be the problem here?

Thanks

Upvotes: 1

Views: 2190

Answers (1)

JLe
JLe

Reputation: 2904

Are you refering to that the file on disk doesn't change? You would have to save the grayscaled bitmap: bm.Save("D:\\bitmap.tif");

Upvotes: 2

Related Questions