LitDev.Engines.FIP.ToBlackwhite C# (CSharp) Method

ToBlackwhite() public method

Converts ARGB image to black and white and inverses it with respect to given threshold
public ToBlackwhite ( Bitmap OriginalImage, int threshold ) : Bitmap
OriginalImage System.Drawing.Bitmap Original ARGB image
threshold int Threshold
return System.Drawing.Bitmap
        public Bitmap ToBlackwhite(Bitmap OriginalImage, int threshold)
        {
            Bitmap OutputImage = new System.Drawing.Bitmap(OriginalImage.Width, OriginalImage.Height);

            if (threshold < 1 || threshold > 254)
            {
                throw new Exception("Threshold value must be in range from 1 to 254");
            }

            for (int x = 0; x < OriginalImage.Width; x++)
            {
                for (int y = 0; y < OriginalImage.Height; y++)
                {
                    Color pixel = OriginalImage.GetPixel(x, y);
                    int gs = (int)((pixel.R * 0.3) + (pixel.G * 0.59) + (pixel.B * 0.11));
                    if (gs > threshold) gs = 255; else gs = 0;
                    Color newColor = Color.FromArgb(255, gs, gs, gs);
                    OutputImage.SetPixel(x, y, newColor);
                }
            }

            return OutputImage;
        }