Convert a Color Image into Black and White Image

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

Original Image

Converted Image

Converted Image

Ebook Download
View all
Learn
View all