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