Introduction
It is a simple way to convert a color image into a black and white image. The
main logic of black and white image is that the each and every pixel have same
and equal value in RBG. So The difference between color and black and white
image the average value of RBG will be the value of R, B and G of the black and
white image.
C# Code
var originalbmp = new Bitmap(Bitmap.FromFile(OFD.FileName)); // Load the image
var newbmp = new Bitmap(Bitmap.FromFile(OFD.FileName)); // New image
for (int row = 0; row < originalbmp.Width; row++) // Indicates row number
{
for (int column = 0; column < originalbmp.Height; column++) // Indicate column number
{
var colorValue = originalbmp.GetPixel(row, column); // Get the color pixel
var averageValue = ((int)colorValue.R + (int)colorValue.B + (int)colorValue.G)/3; // get the average for black and white
newbmp.SetPixel(row, column, Color.FromArgb(averageValue, averageValue, averageValue)); // Set the value to new pixel
}
}
newbmp.Save(OFD.FileName.Replace(".", "_BlackAnd White")); // Save the black ad white image
Process.Start(OFD.FileName.Replace(".", "_BlackAnd White")); // Open the image
}
Original Image
Converted Image


Jeff LePosted Jul 23, 2020, 4:52 PM
The reason we want to convert color to B&W is to reduce the file size, but the B&W file size double the Color. Any ideas?
Princy GuptaPosted Jan 3, 2018, 7:08 AM
If we want to convert image color to other colors except gray scale how could we do this please explain
SHUBHA SAHOOPosted Oct 10, 2017, 5:10 AM
This code converts the image to gray scale, not black and white.