简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM