Convert Color Image Into Gray Scale Image

  • Home -
  • Blog -
  • Convert Color Image Into Gray Scale Image
Convert_Color_Image_Into_Gray_Scale_Image
  • 28 Jun
  • 2019

Convert Color Image Into Gray Scale Image

A gray picture is simply one in which the only colors are gray shades. This is because less information is needed for each pixel to be supplied from any other form of color picture. In fact, a "Gray" color has the same RGB space intensity in the red, green and blue components, so that for each pixel a single intensities value is only specified as opposed to the three intensities required for a full color image in each pixel.

The intensity of grayscale is often stored as an 8-bit integer that provides 256 different shades of gray between black and white. The difference between successive gray levels is much better than the gray-level resolution force in the human eye when levels are evenly spaced.

Example:

Gray-scale Image by C#

The Grayscale is a monochrome (gray) collection or range of shades, ranging from pure white in the lightest end to pure black on the other end. Grayscale includes only data about luminance (brightness) and no colour; That's why the maximum luminance is white and zero black; it's all in the middle of a gray shade. This is why grayscale pictures contain only gray and no color shades.

Remember: Grayscale is also referred to as acromatic.

 

 

 

A pixel can have 255 various gray shades, including black and white, for an 8-bit gryscale picture. When RGB values of a pixel are the same, a shade of gray is developed. These values will be between 0 and 255 and will be shown as a full black color when a pixe holds 0 for the red, the green and the black. And we can see it as a white color if it holds 255 for each of them. Everything in the middle will look like a gray shadow.

 

Using  Microsoft Visual Studio Create a Windows Application project and design the form according to image using controls:

 

Convert Color Image To Gray-scale Image Example

Load image on Picture Box Control  in the form


OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = Environment.CurrentDirectory;
            open.Filter = "Image Files(*.jpg;*.jpeg;)|*.jpg; *.jpeg;";
            if(open.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(open.FileName);
            }

Create method to Convert JPG Color Image to Grayscale JPG Image


private static Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int average = (Convert.ToInt32(c.R) + Convert.ToInt32(c.G) + Convert.ToInt32(c.B)) / 3;
                    bm.SetPixel(x, y, Color.FromArgb(average, average, average));
                }
            }
            return bm;
        }

 

Now, call the methos on Convert button click and display converted image to picturebox


 private void btnConvertImage_Click(object sender, EventArgs e)
        {
          pictureBox1.Image=  ConvertToGrayscale(new Bitmap(pictureBox1.Image));
          pictureBox1.Refresh();
        }

 

 

Finally save the converted image On Save button click 


 private void btnSave_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save("ConvertedGrayscaleImage.jpg", ImageFormat.Jpeg);
            MessageBox.Show("ImageSaved!");
        }

Complete Source Code


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ConvertColorImageToGrayScale
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoadImage_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.InitialDirectory = Environment.CurrentDirectory;
            open.Filter = "Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg; *.jpeg; *.bmp;";
            if(open.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = new Bitmap(open.FileName);
            }
        }

        private void btnConvertImage_Click(object sender, EventArgs e)
        {
          pictureBox1.Image=  ConvertToGrayscale(new Bitmap(pictureBox1.Image));
          pictureBox1.Refresh();
        }
        private static Bitmap ConvertToGrayscale(Bitmap source)
        {
            Bitmap bm = new Bitmap(source.Width, source.Height);
            for (int y = 0; y < bm.Height; y++)
            {
                for (int x = 0; x < bm.Width; x++)
                {
                    Color c = source.GetPixel(x, y);
                    int average = (Convert.ToInt32(c.R) + Convert.ToInt32(c.G) + Convert.ToInt32(c.B)) / 3;
                    bm.SetPixel(x, y, Color.FromArgb(average, average, average));
                }
            }
            return bm;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            pictureBox1.Image.Save("ConvertedGrayscaleImage.jpg", ImageFormat.Jpeg);
            MessageBox.Show("ImageSaved!");
        }
    }
}

Output:

Converted Gray Scale Image Output

 

Convert Color Image To Gray-Scale Image Source Code

 

Video Tutorial